JS to determine if the browser is closed

Source: Internet
Author: User

http://www.blogjava.net/wyz191/archive/2008/12/08/245089.html JSwindow.onunload=function () {
var n=window.event.screenx-window.screenleft;
var b=n>document.documentelement.scrollwidth-20;
if (b&&window.event.clienty<0| | Window.event.altKey) {
Alert ("Off");
}else{
Alert ("Refresh");
}
}

Both Firefox and IE support the onbeforeunload event in the three major browsers, which opera has yet to support.

Usage:

    • Object.onbeforeunload = Handler
    • <element onbeforeunload = "Handler" ... ></element>

Describe:
When the event is triggered, a dialog box with OK and cancel is popped up, and then the page is left, and the cancellation continues on this page.
Handler can set a return value as the display text for the dialog box.

Triggered at:

    • Close the browser window
    • When you go to another page through the address bar or the Favorites folder
    • Click Back, go ahead, refresh, home page one of the time
    • Click on a URL to a different page to connect to the time
    • When invoking any of the following events: Click,document write,document open,document close,window Close, window navigate, window Navigateandfind, Location Replace,location Reload,form Submit.
    • When you open a page with window open and pass the name of the window on this page to the page you want to open.
    • When the value of Location.href is re-assigned.
    • When submitting a form with the specified action via the input type= "submit" button.

Can be used in the following elements:
BODY, FRAMESET, window

Platform Support:
Ie4+/win, Mozilla 1.7a+, Netscape 7.2+, firefox0.9+

The label has only the Onload\onunload\onbeforeunload event, not the OnClose event. The OnUnload event occurs regardless of whether the page is closed or refreshed.      How do I snap to a page close? When the page loads only when the onload page is executed, only the OnUnload page is executed when the onbeforeunload is executed, and then OnUnload, the last onload. This way we can add a tag to the onbeforeunload and judge the tag in the onunload to see if the page is actually closed.

JS to determine if the browser is closed

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.