How to use the onbeforeunload event to detect whether a window is refreshed or closed

Source: Internet
Author: User
Tags connect

Onunload,onbeforeunload is called when the page is refreshed or closed, except that:

onBeforeUnload is triggered before the page is refreshed or closed, and the browser does not request the server to read the new page, so onbeforeunload can prevent the page from being updated or closed.

OnUnload is also triggered when the page is refreshed or closed, but the browser has asked the server to read the new page, so onunload cannot prevent the page from being updated or closed.

1. The onbeforeunload Incident

When an event is triggered, a confirmation and Cancellation dialog box pops up to confirm that the subsequent events will not be performed or remain on this page.

Trigger on:

Close browser window

When you go to another page by using the Address bar or favorite folder

Click Back, Forward, refresh, homepage one of the time

Click on a URL to another page to connect

When you invoke 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 of this page to the page you want to open.

When the value of the LOCATION.HREF is again assigned.

Submit a form with the specified action through the input type= "submit" button.

Can be used in the following elements:

Body, FRAMESET, window

Platform support: Ie,firefox,chrome,safari. Opera does not support the temporary.

Example:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >  

2. The OnUnload incident

When an event is triggered, a confirmation dialog box pops up and the point is determined to perform subsequent events.

Trigger on:

Close browser window

When you go to another page by using the Address bar or favorite folder

Click Back, Forward, refresh, homepage one of the time

Click on a URL to another page to connect

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/script/

When you invoke 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 of this page to the page you want to open.

When the value of the LOCATION.HREF is again assigned.

Submit a form with the specified action through the input type= "submit" button.

Platform support: Only IE, no other support.
Example:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >  

Author: csdn blog proud Snow star Maple

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.