Home > Web Front-end > JS Tutorial > How Can I Detect Page Reloads or Refreshes in JavaScript?

How Can I Detect Page Reloads or Refreshes in JavaScript?

Linda Hamilton
Release: 2024-12-17 22:41:11
Original
698 people have browsed it

How Can I Detect Page Reloads or Refreshes in JavaScript?

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");
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template