In the realm of web development, knowing when a user reloads or refreshes a page can be crucial for various scenarios. In this article, we'll explore ways to effectively check for page reload events using JavaScript.
The navigator object provides us with access to the Navigation Timing API, which empowers us to determine how the page was loaded.
if (window.performance) {<br> console.info("window.performance works fine on this browser");<br>}<br>console.info(performance.navigation.type);<br>if (performance.navigation.type == performance.navigation.TYPE_RELOAD) {<br> console.info("This page is reloaded");<br>} else {<br> console.info("This page is not reloaded");<br>}
It's important to note that the window.performance.navigation.type API is deprecated. I recommend using the improved approach outlined by Илья Зеленько.
The above is the detailed content of How Can I Efficiently Detect Page Reloads in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!