【问题标题】:How to detect browser force refresh event - Cross Browser如何检测浏览器强制刷新事件 - 跨浏览器
【发布时间】:2019-12-03 13:13:38
【问题描述】:

我想检测 JavaScript 上的强制刷新事件。当用户单击浏览器重新加载按钮或按 Ctrl + f5(在 Windows 上)/Cmd + r(在Mac)和移动设备上的刷新

我尝试了beforeunload 事件,但此事件在每次页面加载并导航到其他页面时触发。

window.addEventListener('beforeunload', function (event) {
   // This not working exactly what I want.
})

【问题讨论】:

    标签: javascript cross-browser dom-events


    【解决方案1】:

    我能想到的最佳解决方案是Navigation Timing

    window.performance 属性为性能相关属性提供了一个托管区域。

    if (window.performance) {
      if (performance.navigation.type === performance.navigation.TYPE_RELOAD) {
        console.info( "Reload detected" );
      } else {
        console.info( "Reload not detected");
      }
    } else {
      console.info("window.performance is not supported");
    }

    【讨论】:

      【解决方案2】:

      试试这个:

      if (performance.navigation.type == 1) {
              console.info( "This page is reloaded" );
      }
      

      【讨论】:

        猜你喜欢
        • 2014-11-06
        • 2011-09-30
        • 1970-01-01
        • 2011-10-11
        • 2018-08-24
        • 2011-03-11
        相关资源
        最近更新 更多