关闭页面之前 提示用户(确定跳转,取消停留)----使用window.onbeforeunload

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: 

Java代码   收藏代码
  1. var _isset=0;  
  2.   
  3. function demo() {  
  4.    window.onbeforeunload = function () {  
  5.       if (_isset==0) {  
  6.          _isset=1;  // This will only be seen elsewhere if the user cancels.  
  7.          return "This is a demonstration, you won't leave the page whichever option you select.";  
  8.       }  
  9.    }  
  10.    _isset=0;  
  11.    window.location.reload();  
  12.    return false;  
  13. }  

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 could ever be one is if the user previously aborted a page unload. 

But as you can see this method won't help you if you need to be immediately notified that 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). 
-------------------------------------------------------------- 
虽然如此,但还是有高手给出了如下代码 ^^ 

Java代码   收藏代码
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"  
  2.    "http://www.w3.org/TR/html4/strict.dtd">  
  3. <html><head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  
  5. <meta http-equiv="Content-Style-Type" content="text/css">  
  6. <meta http-equiv="Content-Script-Type" content="text/javascript">  
  7. <title>onbeforeunload test</title>  
  8. <script type="text/javascript"><!--  
  9. window.onbeforeunload = function() {  
  10.   // in Firefox and Netscape 7.2+ the setTimeout or setInterval do not wait   
  11.   // to be executed until after the user clicks one of the buttons in the   
  12.   // confirm()-like box.  
  13.   
  14.   //setTimeout("alert('hi from setTimeout()');",500);  
  15.   // setTimeout() and setInterval() aren't called when ok is clicked in   
  16.   // IE5-6/Win, but is called in IE7 when the time is short, but not when   
  17.   // it's longer, like 500 (a half second).  
  18.   window.unloadTimer = setInterval(  
  19.     "alert('hi from setInterval()');clearInterval(window.unloadTimer);",500);  
  20.   window.onunload = function() {clearInterval(window.unloadTimer);}  
  21.   return 'onbeforeunload testing';  
  22. }  
  23. // -->  
  24. </script>  
  25. </head>  
  26. <body>  
  27.   
  28. <h1>onbeforeunload test</h1>  
  29.   
  30. </body>  
  31. </html>  

Java代码   收藏代码
  1. <script type="text/javascript">  
  2. //<![CDATA[  
  3. var  
  4.   is_asked = false;  
  5.   
  6. window.onbeforeunload =   
  7.   function (ev) {  
  8.     var e = ev || window.event;  
  9.     window.focus();  
  10.     if (!is_asked){  
  11.       is_asked = true;  
  12.       var showstr = "CUSTOM_MESSAGE";  
  13.       if (e) {  //for ie and firefox  
  14.         e.returnValue = showstr;   
  15.       }  
  16.       return showstr; //for safari and chrome  
  17.     }  
  18.   };  
  19.   
  20. window.onfocus =  
  21.   function (ev){  
  22.     if (is_asked){  
  23.       window.location.href = "http://www.google.com";  
  24.     }  
  25.   }  
  26.   
  27. //]]>  
  28. </script  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值