You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chrome中检测历史导航(后退/前进)的方法失效问题求助

问题:Chrome中无法检测通过历史后退/前进进入页面的场景

我遇到的问题和强制页面在浏览器后退时重载的同类问题相似,需要区分两种用户操作场景:

  • 用户正常导航至当前页面或刷新页面:页面已正常加载,无需额外操作
  • 用户通过历史后退或前进按钮进入当前页面:页面从浏览器缓存取出未重新加载,需执行window.location.reload()操作

我尝试了相关解决方案,在Firefox(105.0.1)和Edge(105.0.1343.50)中可行,但在Chrome(105.0.5195.127)中无效。

以下是我设计的最简测试代码:

page1.html:

<!doctype html>
<html>
<head>
  <meta charset="utf-8" />
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  <script>
    $(window).on("pageshow", function(e){

      $("#type").text(window.performance.getEntriesByType("navigation")[0].type);
      let historyTraversal = event.persisted
        || ( typeof window.performance != "undefined" && window.performance.getEntriesByType("navigation")[0].type === "back_forward" );
      if ( historyTraversal ) { // Handle page restore.
        $("#backOrNot").text('User just came back using the history "back" button');
        //window.location.reload(); // this is actually the targetted action: reload page
      }
      else {
        $("#backOrNot").text('User loaded the page');
      }

    });
  </script>
</head>
<body>
  <p id="backOrNot"></p>
  <p>( window.performance.getEntriesByType("navigation")[0].type = <span id="type"></span> )</p>
  <p><a href="page2.html">Just a link from where you can click the history back button</a></p>
</body>
</html>

page2.html:

<!doctype html>
<html>
<head>
  <meta charset="utf-8" />
</head>
<body>
  <p>
    From here, try to:<br>
    1. Navigate to <a href="page1.html">page1.html</a><br>
    2. Go back to page1.html using the history back-button
  </p>
</body>
</html>

在Firefox和Edge中,通过历史返回page1时能获取到back_forward状态,但Chrome中仍显示初始加载时的navigate或reload状态,仿佛从未离开再返回。请问我哪里遗漏或操作有误?


内容的提问来源于stack exchange,提问作者fpierrat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 05:15:41