Home > Web Front-end > JS Tutorial > JS code to determine whether the page is closed or refreshed_Basic knowledge

JS code to determine whether the page is closed or refreshed_Basic knowledge

WBOY
Release: 2016-05-16 19:20:00
Original
1201 people have browsed it

The

tag only has onloadonunloadonbeforeunload event but no onclose event. The onunload event will be executed regardless of whether the page is closed or refreshed. How to capture the page closing?​

Only execute onload when the page is loaded

Only execute onunload when the page is closed

When the page is refreshed, onbeforeunload is executed first, then onunload, and finally onload. In this way, we can add a mark in onbeforeunload and judge the mark in onunload to determine whether the page is really closed.



Determine whether the page is closed or refreshed





<script> <br> var s = "test"; <br> function fclose() <br> { <br> if(s=="no") <br> alert(&#146;unload me!=&#146;+s+&#146;这是刷新页面!&#146;); <br> else <br> alert(&#146;这是关闭页面&#146;); <br> } <br><br> function fload() <br> { <br> alert("load me!="+s); <br> } <br><br> function bfunload() <br> { <br> s = "no"; <br> } <br> </script>
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template