Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

javascript - 检查页面是否在JavaScript中重新加载或刷新(Check if page gets reloaded or refreshed in 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 it should display an alert.

(例如,当我打开一个页面时没有任何反应,但是当我刷新页面时,它应该显示一个警告。)

  ask by Ahmed translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

It uses the Navigation Timing API .

(它使用Navigation Timing 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"); } 

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...