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(’unload me!=’+s+’这是刷新页面!’);
<br> else
<br> alert(’这是关闭页面’);
<br> }
<br><br> function fload()
<br> {
<br> alert("load me!="+s);
<br> }
<br><br> function bfunload()
<br> {
<br> s = "no";
<br> }
<br> </script>