Trigger conditions when closing a page a lot of online, but there's hardly any real use, But the basic idea is to use window.onbeforeunload to achieve, but because the browser version is different, whether to support the small page, refresh and shutdown will call the onbeforeunload and OnUnload methods, resulting in most of the online data can not effectively determine whether the current page is refreshed or closed.
The following methods, at least IE6,IE7,IE8 support
Window.onbeforeunload=function ()
{
if ((Event.clientx > document.body.clientwidth-20 && event.clienty<0) | | (! (event.clienty<-30) && (event.clienty <0) | | Event.altkey)
{
Alert ("Close page");
}
else{
Alert ("Refresh page");
}
}
This method if the condition does not determine the key F5 refresh, if necessary can add themselves, because the system requires shielding F5 button refresh, so I do not consider.
In fact, this method is very simple, the principle is to determine the location of the mouse:
Event.clientx: The x-axis position of the mouse in IE
Event.clienty: The y-axis position of the mouse in IE
Document.body.clientWidth: Client IE width
When clicking on the Red Fork in IE's upper right corner, use (Event.clientx > document.body.clientwidth-20 && event.clienty<0) to judge, which Event.clientx > Document.body.clientwidth-20 said the mouse in the IE Close button Red Fork vertical This column, event.clienty<0 and said the mouse position in IE above, so can be judged to be clicked off the operation; event.clienty<-30) && (Event.clienty <0) is a IE7, 8, such as a small page check to make judgments, indicating that the mouse on IE on the top of a small page signed this row to do the operation, this row is not the refresh button, So it can be judged that the point of closing a small page sign.
This method is not valid for roaming, other browsers do not do verification, someone has done the verification words hope to improve this method.