且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查页面是否在Javascript中重新加载或刷新

更新时间:2023-10-31 11:12:16

了解页面实际重新加载的更好方法是使用大多数现代浏览器支持的导航器对象。

A better way to know that the page is actually reloaded is to use the navigator object that is supported by most modern browsers.

它使用导航时间API

//check for Navigation Timing API support
if (window.performance) {
  console.info("window.performance works fine on this browser");
}
  if (performance.navigation.type == 1) {
    console.info( "This page is reloaded" );
  } else {
    console.info( "This page is not reloaded");
  }

来源: https://developer.mozilla.org/en-US/docs/Web/API/Navigation_timing_API