最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is it possible to use the back button to go back to the previous page from a page that was moved by location.href w

programmeradmin1浏览0评论

Here is 1.html

<!DOCTYPE html>
<html>
  1
  <script>
    let time = 0;
    setInterval(() => {
      if (time === 500) location.href = '/2.html';
      time++;
    }, 1);
  </script>
</html>

And here is 2.html

<!DOCTYPE html>
<html>
  <body>
    2
  </body>
</html>

If access to 1.html, according to setInterval(), the page will be moved to 2.html 0.5 seconds later by location.href new value.
However, it is not possible to go back to 1.html using back button on a browser.
I've heard that the reason of it is location.href was executed not by user events (like clicking) but by force caused by my code.
I want to know how to make it work!

I tried history.pushState method to make the browser know that there is one more page you(browser) should add to the stack.
It worked if I go back to previous page by history.back().
However, it still didn't work if I tried to go back to previous page by pressing go back button on a browser.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论