Share JavaScript to get web page close and cancel events off

Source: Internet
Author: User

In the web development, we often use the page closure event onbeforeunload, can give the user a choice to abandon the closure of the opportunity, such as this blog editor. If the user chooses to leave, then the OnUnload event will naturally trigger, but how to detect if the user chooses to cancel?

We assume that a page leaves the cancellation event, called Onunloadcancel. Obviously, this event should be triggered after the user presses the Cancel button of the dialog box. But the triggering process for closing the prompt dialog box is not that simple. Let's review this process first:

Copy CodeThe code is as follows:
function () {    return "really left?" ;}

The onbeforeunload event is triggered when the user is ready to leave the page (such as pressing the Close button, or refreshing the page, and so on). Our script cannot decide whether or not to block the closing of the page in this event, the only thing that can be done is to return a string that appears only as an explanatory text in the Close selection dialog, which the user can choose to close or not close. But we have no way of knowing which one to choose.

But careful analysis of the problem is not the case. If the user really chooses to close the page, then all the running code is Byebye, and if it stays on the page, it will be when nothing happens, except the onbeforeunload event. So, we do a little trick in the onbeforeunload event, which registers a few milliseconds after the start of the timer, if the page is really closed, then the timer is void, then the page is still, a few milliseconds delay for this is an asynchronous interface interaction event also has no error.

Copy CodeThe code is as follows:
<script language= "JavaScript" >function() {    ten);     return "Really leave?"  function() {    alert ("Cancel Away");} </script>

We use settimeout, delay 10ms to execute onunloadcancel. If the page is really closed, the timer will certainly be destroyed; But in the test, we found that Firefox has a two bug:

Sometimes pressing the Close button also executes the Onunloadcancel, and a dialog box flashes past. If you change to a while (1), the browser will continue to die, which means that onunloadcancel is actually executing, just destroying the interface, but not pausing the script to run.
If you leave by refreshing the page, only once onbeforeunload is executed, but clicking the X button closes the page and executes two times onbeforeunload. So we need to refine it to be compatible with FF.

Copy CodeThe code is as follows:
<script language= "JavaScript" >varfunction() {    setTimeout (  function() {_t = SetTimeout (onunloadcancel, 0)}, 0);     return "Really leave?"  function() {    cleartimeout (_t);    Alert ("Cancel Departure"); </script>

Here used a way I also can not say why, should be considered hack, solve the FF bug.

The above transfer from the script house, share to the needs of friends.

Share JavaScript to get web page close and cancel events off

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.