About the cancellation event that captures when a user clicks window. onbeforeunload

Source: Internet
Author: User

Detecting When The User Has Clicked Cancel
One of the things you may want to do is to be notified when the user clicks cancel, aborting a page unload. unfortunately there's no way to be immediately notified. the best you can do is to set a unique global variable in your "onbeforeunload" event and then look to see if that variable has been set in other functions. there is no way to get an immediate notification that the user has aborted a page unload.
The example code I used above to do an example of an "onbeforeunload" dialog is as follows:
Copy codeThe Code is as follows:
Var _ isset = 0;

Function demo (){
Window. onbeforeunload = function (){
If (_ isset = 0 ){
_ Isset = 1; // This will only be seen elsewhere if the user cancels.
Return "This is a demonstration, you won't leave the page whichever option you select .";
}
}
_ Isset = 0;
Window. location. reload ();
Return false;
}

This code defines a global variabled named _ isset, and then initializes it to zero. in our "onbeforeunload" event the variable is checked and if it's set to one, no unload dialog box will appear. the only way _ isset cocould ever be one is if the user previusly aborted a page unload.

But as you can see this method won't help you if you need to be immediately notified that the user has finished dealing with the confirmation box. you can detect when it appears on the screen but there's no way to know when the user has finished interacting with it if the user clicked cancel (if the user clicked OK, then of course the unload event will have been tripped ).
--------------------------------------------------------------
Despite this, some experts have provided the following code ^
Copy codeThe Code is as follows:
<! Doctype html public "-// W3C // dtd html 4.01 // EN"
Http://www.w3.org/TR/html4/strict.dtd>
<Html> <Meta http-equiv = "Content-Type" content = "text/html; charset = iso-8859-1">
<Meta http-equiv = "Content-Style-Type" content = "text/css">
<Meta http-equiv = "Content-Script-Type" content = "text/javascript">
<Title> onbeforeunload test </title>
<Script type = "text/javascript"> <! --
Window. onbeforeunload = function (){
// In Firefox and Netscape 7.2 + the setTimeout or setInterval do not wait
// To be executed until after the user clicks one of the buttons in
// Confirm ()-like box.

// SetTimeout ("alert ('Hi from setTimeout () ');", 500 );
// SetTimeout () and setInterval () aren't called when OK is clicked in
/// IE5-6/Win, but is called in IE7 when the time is short, but not when
// It's longer, like 500 (a half second ).
Window. unloadTimer = setInterval (
"Alert ('Hi from setInterval () '); clearInterval (window. unloadTimer);", 500 );
Window. onunload = function () {clearInterval (window. unloadTimer );}
Return 'onforeunload testing ';
}
// -->
</Script>
</Head>
<Body>
<H1> onbeforeunload test </Body>
</Html>

Copy codeThe Code is as follows:
<Script type = "text/javascript">
// <! [CDATA [
Var
Is_asked = false;

Window. onbeforeunload =
Function (ev ){
Var e = ev | window. event;
Window. focus ();
If (! Is_asked ){
Is_asked = true;
Var showstr = "CUSTOM_MESSAGE ";
If (e) {// for ie and firefox
E. returnValue = showstr;
}
Return showstr; // for safari and chrome
}
};

Window. onfocus =
Function (ev ){
If (is_asked ){
Window. location. href = "http://www.google.com ";
}
}

//]>
</Script

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.