How to make an HTML back link?

后端 未结 12 834
臣服心动
臣服心动 2020-12-12 08:32
12条回答
  •  一个人的身影
    2020-12-12 09:03

    history.go(-1) doesn't work if you click around in the 2nd domain or if the referrer is empty.

    So we have to store the historyCount on arriving to this domain and go back the number of navigations in this side minus 1.

    
    // if referrer is different from this site
    if (!document.referrer.includes(window.location.host)) {
      // store current history length
      localStorage.setItem('historyLength', `${history.length}`);
    }
    
    // Return to stored referrer on logo click
    document.querySelector('header .logo').addEventListener('click', 
      () =>
       history.go(Number(localStorage.getItem('historyLength')) - history.length -1)
    );
    

提交回复
热议问题