Browser support for OnUnload events differs from trigger condition implementation

Source: Internet
Author: User

Standard reference

The description of the OnUnload event in the HTML 4.01 specification is that the OnUnload event is triggered when document is removed from the window.

About the OnUnload intrinsic event description in the HTML 4.01 specification: Http://www.w3.org/TR/html401/interact/scripts.html#adef-onunload.

Description of the OnUnload event in MSDN: http://msdn.microsoft.com/en-us/library/ms536973 (vs.85). aspx.

Description of the OnUnload event in Mozilla Developer Center: https://developer.mozilla.org/en/DOM/window.onunload.

Problem description

When document is removed from the window, the OnUnload event is triggered, and the browser support for the OnUnload event differs from the trigger condition implementation.

The impact

Each browser's support for OnUnload events differs from the trigger condition implementation, so the methods written in the OnUnload event may not be executed in some browsers.

The affected browser
All browsers
Problem analysis

As described in MSDN, the OnUnload event for IE can be triggered by the following conditions:

    • Close the current browser window.
    • Navigate to another to enter a new address or choose a preferred location.
    • Click Back, Forward, refresh, or home button.
    • Click on a link to a new page.
    • Call the Anchor.click method.
    • Call the Document.Write method.
    • Call the Document.open method.
    • Call the Document.close method.
    • Call the Window.close method.
    • Call the Window.Open method, and the window name setting value is _self.
    • Call the Window.navigate or Navigateandfind method.
    • Call the Location.replace method.
    • Call the Location.reload method.
    • Specifies a new value for the Location.href property.
    • Use the Submit button to submit the form, or call the Form.submit method.

Most of these trigger conditions start with a jump in the page, but there is a lack of a common scenario where the page URL may have changed but no jumps have occurred. For example, "Javascipt:" "mailto:" and other common browser built-in pseudo-protocol, as well as by a third party or user-defined as a protocol, the page does not jump, but according to the pseudo-protocol to perform the specified behavior. This condition should be added to the trigger condition.

Based on all of these trigger conditions, we construct the following code to detect the level of support and trigger conditions for each browser for the OnUnload event:

<! DOCTYPE Html>

Summary of execution results into the table:

IE6 IE7 IE8 Firefox Chrome Safari Opera
Close the current browser window Event is triggered Event not triggered Event not triggered Event is triggered Event is triggered Event not triggered Event not triggered
Navigate to another to enter a new address or choose a favorite location Event is triggered Event is triggered Event is triggered Event is triggered Event is triggered Event is triggered Event not triggered
Click Back, Forward, refresh, or home button Event is triggered Event is triggered Event is triggered Event is triggered Event is triggered Event is triggered Event not triggered
Click on a link to a new page Event is triggered Event is triggered Event is triggered Event is triggered Event is triggered Event is triggered Event is triggered
Calling the Anchor.click method Event is triggered Event is triggered Event is triggered This method is not supported 1 This method is not supported 1 This method is not supported 1 Event is triggered
Calling the Document.Write method Event is triggered Event is triggered Event is triggered Event not triggered Event not triggered Event not triggered Event not triggered
Calling the Document.open method Event is triggered Event is triggered Event is triggered Event not triggered Event not triggered Event not triggered Event not triggered
Calling the Document.close method Event not triggered Event not triggered Event not triggered Event not triggered Event not triggered Event not triggered Event not triggered
Call the Window.Open method, and the window name setting value is _self Event is triggered Event is triggered Event is triggered Event is triggered Event is triggered Event is triggered Event is triggered
Call Window.navigate Event is triggered Event is triggered Event is triggered This method is not supported 2 This method is not supported 2 This method is not supported 2 Event not triggered
Calling the Navigateandfind method Event is triggered Event is triggered Event is triggered This method is not supported 3 This method is not supported 3 This method is not supported 3 This method is not supported 3
Calling the Location.replace method Event is triggered Event is triggered Event is triggered Event is triggered Event is triggered Event is triggered Event not triggered
Calling the Location.reload method Event is triggered Event is triggered Event is triggered Event is triggered Event is triggered Event is triggered Event not triggered
Specify a new value for the Location.href property Event is triggered Event is triggered Event is triggered Event is triggered Event is triggered Event is triggered Event is triggered
Submit a form using the Submit button Event is triggered Event is triggered Event is triggered Event is triggered Event is triggered Event is triggered Event is triggered
Calling the Form.submit method Event is triggered Event is triggered Event is triggered Event is triggered Event is triggered Event is triggered Event is triggered
Call Javascipt: Pseudo-protocol Event not triggered Event not triggered Event not triggered Event not triggered Event not triggered Event not triggered Event not triggered
Call mailto: pseudo-protocol Event not triggered Event not triggered Event not triggered Event not triggered Event not triggered Event not triggered Event not triggered
Invoking a custom pseudo-protocol Event is triggered Event not triggered Event not triggered Event not triggered Event not triggered Event is triggered Event not triggered

"Note 1": The click Method of directly invoking the link element simulates mouse click events, only IE and Opera support.

"Note 2": Using the Window.navigate method to navigate the page is only supported by IE Opera, you can refer to the MSDN Original: Navigate method.

"Note 3": The Navigateandfind method is in the Window.External object, external object is also only IE support, you can refer to the MSDN Original: Navigateandfind method and the article Bt9012:ie E The Xternal object provides methods that are specific to IE.

In conjunction with the summary table, you can see:

    • Click on a link to a new page, call the Window.Open method window name to set the value to _self, specify a new value for the Location.href property, submit the form using the Submit key, call the Form.submit method under each browser onunload Events will be triggered.
    • In other cases, each browser is different.
Solution Solutions

There are many differences between browser support and event triggering conditions, so use caution.

Browser support for OnUnload events differs from trigger condition implementation

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.