Erase session when JavaScript closes page

Source: Internet
Author: User
Tags close page

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.