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