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

How Can I Efficiently Detect Page Reloads in JavaScript?

Susan Sarandon
Susan SarandonOriginal
2024-12-11 21:14:15564browse

How Can I Efficiently Detect Page Reloads in JavaScript?

Efficiently Detecting Page Reload Events with JavaScript

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.

Utilizing the window.performance API

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>}

Deprecation Notice

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!

Statement:
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