Home > Article > Web Front-end > How to Distinguish Between Page Refresh and Browser Closure in JavaScript?
Identifying Page Refresh vs. Browser Closure
When triggering the ONUNLOAD event, it becomes difficult to distinguish between refreshing a page and closing the browser. This article presents a solution to this problem.
Solution
This solution utilizes HTML5's local storage and server-client AJAX communication:
Window Unload Handler:
Body Load Handler:
Server Handling:
This approach distinguishes between tab/window closures and page reloads by considering the timing of unload events. It is applicable to other events like followed links and submitted forms by placing event handlers on relevant pages.
Limitations:
This solution requires HTML5 local storage support and may not work on older browsers like MSIE7.
The above is the detailed content of How to Distinguish Between Page Refresh and Browser Closure in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!