Program
The code is as follows |
Copy Code |
Window.onunload = function () { var a_n = window.event.screenx-window.screenleft; var a_b = a_n > document.documentelement.scrollwidth-20; if (a_b && window.event.clienty< 0 | | Window.event.altKey) { alert (' close page behavior '); }else{ alert (' Jump or Refresh page behavior '); } } |
It works with the Close button in the upper-right corner of the browser, but it doesn't work when you close the tab and close it on the taskbar.
JS tags only onloadonunloadonbeforeunload events, and no onclose events.
The OnUnload event is performed regardless of whether the page is closed or refreshed.
How do I capture a page close?
The onload is only executed when the page loads
Only perform onunload when page closes
When the page is refreshed, execute onbeforeunload First, then onunload, and finally onload.
So we can add a tag in the onbeforeunload and judge the tag in the onunload to see if the page really shuts down.
More complete, compatible FF
The code is as follows |
Copy Code |
<mce:script type= "Text/javascript" ><!-- function Close (evt)//author:sunlei { var Isie=document.all?true:false; EVT = evt? EVT:(window.event? Window.event:null); if (Isie) {//ie browser var n = evt.screenx-window.screenleft; var b = n > document.documentelement.scrollwidth-20; if (b && evt.clienty<0 | | evt.altkey) { Alert ("is off rather than refreshed"); Window.location.href= ". /include/logout.php "; } else{ Alert ("is refreshed rather than closed"); return false; } } else{//Firefox browser if (document.documentelement.scrollwidth!=0) { Alert ("is refreshed rather than closed"); Window.location.href= "Report_list.php?ss=1"; return false; } else{ Alert ("is off rather than refreshed"); Window.location.href= "Repost_list.php?ss=0"; Alert ("BBBBBBB"); } } } --></mce:script> <body onunload= "Close (event);" > |
The above method has no way to judge the browser of multiple tabs, such as 360,IE8, see below
The code is as follows |
Copy Code |
function Closeopen (event) { if (event.clientx<=0 | | event.clienty<0) { Get current time var date=new date (); Set Date to past time Alert ("Close page"); Date.settime (Date.gettime ()-10000); Remove UserID This cookie Document.cookie= "zhuangtao;expire=" +date.toutcstring (); Document.cookie= "quanxianzifucuan;expire=" +date.toutcstring (); Document.cookie= "quanxian;expire=" +date.toutcstring (); S0 + = "close window!"; SW = 1; onBeforeUnload (); Window.event.returnValue = ' Close browser will exit system. ' } Else { Alert ("Refresh or leave"); } } var currentkeycode =-1; function Document.onkeydown () {//All subordinate pages of this window must contain the functions Top.currentkeycode = Event.keycode; } function onbeforeunload () {
var sw = 0, S0 = ""; if (Currentkeycode = 116) { S0 = "Refresh Window!" (F5) "; } Else { if ((Event.altkey) && (Currentkeycode = 115)) { S0 = "Close the window!" (ALT+F4) "; SW = 1; Get current time var date=new date (); Set Date to past time Alert ("Close window"); Date.settime (Date.gettime ()-10000); Remove UserID This cookie Document.cookie= "zhuangtao;expire=" +date.toutcstring (); Document.cookie= "quanxianzifucuan;expire=" +date.toutcstring (); Document.cookie= "quanxian;expire=" +date.toutcstring (); } Else { if ((Event.clientx > 0) && (Event.clientx < document.body.clientWidth)) { S0 + + "Refresh window!"; } Else { Get current time var date=new date (); Set Date to past time Alert ("Close page"); Date.settime (Date.gettime ()-10000); Remove UserID This cookie Document.cookie= "zhuangtao;expire=" +date.toutcstring (); Document.cookie= "quanxianzifucuan;expire=" +date.toutcstring (); Document.cookie= "quanxian;expire=" +date.toutcstring (); S0 + = "close window!"; SW = 1; } } } if (sw = 1) { Event.returnvalue = ""; } Else { Currentkeycode =-1; }
} <body onunload= "Closeopen (event)" ></body> |
The above only can not be used in the taskbar closed, basic to meet our requirements.