The principle is to detect the window size of the browser at this time by leaving the page behavior time onunload triggering time. According to the size, it is determined that the user is refreshing, jumping or closing the behavior program.
The code is as follows
= function(){ var a_n = - ; var a_b = a_n > -20; if(a_b && < 0 || ){ alert('Close page behavior'); }else{ alert('Jump or refresh the page behavior'); } }
It is easy to use when using the close button in the upper right corner of the browser, but it doesn't work if it is closed on the tab and the taskbar.
The js tag has only the onloadonunloadonbeforeunload event, but no onclose event.
The onunload event will be executed regardless of whether the page is closed or refreshed.
How to catch the page closing?
Only onload is executed when the page is loaded
Only onunload is executed when the page is closed
When the page is refreshed, first execute onbeforeunload, then onunload, and finally onload.
In this way, we can add a mark to the onbeforeunload and judge the mark in the onunload to determine whether the page is really closed.
More complete compatibility
The code is as follows
<mce:script type="text/javascript"><!-- function close(evt) //author: sunlei { var isIE=?true:false; evt = evt ? evt :( ? : null); if(isIE){//IE browser var n = - ; var b = n > -20; if(b && <0 || ){ //alert("is to close, not to refresh"); ="../include/"; } else{ //alert("Refresh, not close"); return false; } } else{//Firefox Browser if(!=0) { //alert("Refresh, not close"); //="report_list.php?ss=1"; return false; } else{ alert("It's closing, not refreshing"); //="repost_list.php?ss=0"; //alert("bbbbbbb"); } } } // --></mce:script> <BODY onunload="close(event);">
The above method cannot determine multi-tab browsers, such as 360, ie8. Let's look at it below
The code is as follows
function CloseOpen(event) { if(<=0 || <0) { //Get the current time var date=new Date(); //Set date to past time alert("Close the web page"); (()-10000); //Delete the userId cookie ="zhuangtao;expire="+(); ="quanxianzifucuan;expire="+(); ="quanxian;expire="+(); s0 += "Close the window!"; sw = 1; onbeforeunload(); // = 'Close the browser will exit the system.'; } else { alert("Refresh or leave"); } } var currentKeyCode = -1; function () { // All subordinate pages of this window must contain this function = ; } function onbeforeunload(){ var sw = 0, s0 = ""; if (currentKeyCode == 116) { s0 += "Refresh the window!(F5)"; } else { if (()&&(currentKeyCode == 115)) { s0 += "Close the window!(alt+F4)"; sw = 1; //Get the current time var date=new Date(); //Set date to past time alert("Close the window"); (()-10000); //Delete the userId cookie ="zhuangtao;expire="+(); ="quanxianzifucuan;expire="+(); ="quanxian;expire="+(); } else { if (( > 0)&&( < )) { s0 += "Refresh the window!"; } else { //Get the current time var date=new Date(); //Set date to past time alert("Close the web page"); (()-10000); //Delete the userId cookie ="zhuangtao;expire="+(); ="quanxianzifucuan;expire="+(); ="quanxian;expire="+(); s0 += "Close the window!"; sw = 1; } } } if (sw == 1) { = ""; } else { currentKeyCode = -1; } } <body onunload="CloseOpen(event)" ></body></html>
The above can only be closed on the taskbar, which can basically meet our requirements.
The above content is the JavaScript introduced to you in this article to determine whether the web page is closed or refreshed. I hope you like it.