检查页面是否在Javascript中重新加载或刷新
问题描述:
我想检查某人是否有人试图刷新页面。
I want to check when someone tries to refresh a page.
例如,当我打开一个页面时没有任何反应,但是当我刷新页面时会返回一个警告。
For example, when I open a page nothing happens but when i refresh the page returns an alert.
答
了解页面实际重新加载的更好方法是使用大多数现代浏览器支持的导航器对象。
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