如何检测浏览器强制刷新事件 - Cross Browser

How to detect browser force refresh event - Cross Browser

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

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

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

试试这个:

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

我能想到的最佳解决方案是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");
}