Onbeforeunload conflicts with a tag in IE bug

Source: Internet
Author: User

Onbeforeunload is an event of window. Currently, Firefox and IE support this event. It is mainly used to indicate whether the user really wants to leave the page. Generally, before some important data is submitted, prevents data loss due to user misoperations. In typical applications such as gmail, If you refresh the page or close the page when writing an email, a prompt is displayed.

However, when you click a label under IE, the onbeforeunload event is also triggered. In addition, javascript: void (0) cannot be written in href, but similar situations will not occur in Firefox. So I checked the information and got to know the onbeforeunload event again:

Sequence of events triggered by tag
The operational relationship between onclick, onbeforeunload, and href is as follows: onclick> onbeforeunload> href. By knowing this, we can block onbeforeunload in some ways. In the IE browser, if the href is #, The onbeforeunload event will not be triggered.

How to stop onbeforeunload
At the same time of Ajax, add an onclick event to the tag so that onclick runs before onbeforeunload and returns false ~

Bypassing onbeforeunload direct href
In combination with the onclick event, we can bypass onbeforeunload to directly href. The following code can bypass onbeforeunload and execute href:

Var a = 1;
Window. onbeforeunload = function ()
{
If (a) alert ("onbeforeunload event broke out! ");
}
You only need to add a = 0; To The onclick event ~

Instance
Here we use window. onbeforeunload to test onclick, onbeforeunload, and href. Please test in IE:

Test # | test connection | test Click before onbeforeunload | test onbeforeunload before href | avoid onbeforeunload direct href

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.