How to Determine Page Reload or Refresh in JavaScript
Problem:
Detect when a user reloads or refreshes a web page. For instance, when the page is initially opened, it should remain unchanged, but upon refreshing, an alert should be triggered.
Solution:
It's crucial to note that window.performance.navigation.type is deprecated. Instead, utilize the navigator object supported by most modern browsers, which leverages the Navigation Timing API.
// Check for Navigation Timing API support if (window.performance) { console.info("window.performance works fine on this browser"); } console.info(performance.navigation.type); if (performance.navigation.type == performance.navigation.TYPE_RELOAD) { console.info("This page is reloaded"); } else { console.info("This page is not reloaded"); }
The above is the detailed content of How Can I Detect Page Reloads or Refreshes in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!