关于捕获用户何时点击window.onbeforeunload的取消事件

时间:2024-04-22 22:45:07 

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:


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 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).
--------------------------------------------------------------
虽然如此,但还是有高手给出了如下代码 ^^


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<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 the
// 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 'onbeforeunload testing';
}
// -->
</script>
</head>
<body>
<h1>onbeforeunload test</h1>
</body>
</html>



<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
标签:onbeforeunload,取消事件
0
投稿

猜你喜欢

  • 用于WebKit的CSS诀窍[译]

    2009-03-11 20:03:00
  • 本地计算机无法启动Apache故障处理

    2023-07-17 13:06:48
  • asp中常用的字符串安全处理函数集合(过滤特殊字符等)

    2011-02-20 10:40:00
  • 如何解决springboot数据库查询时出现的时区差异问题

    2024-01-26 01:53:01
  • Python 实现Windows开机运行某软件的方法

    2021-05-15 11:48:42
  • Numpy 将二维图像矩阵转换为一维向量的方法

    2022-09-21 03:11:01
  • oracle中的trim函数使用介绍

    2023-07-11 00:37:08
  • asp随机产生注册用户密码

    2007-10-17 12:33:00
  • Kettle下载与安装保姆级教程(最新)

    2023-07-29 17:10:41
  • python单向链表的基本实现与使用方法【定义、遍历、添加、删除、查找等】

    2021-08-24 03:26:17
  • Python Flask框架模块安装级使用介绍

    2023-05-06 19:28:27
  • 使用python图形模块turtle库绘制樱花、玫瑰、圣诞树代码实例

    2022-07-11 05:28:15
  • python 中文乱码问题深入分析

    2023-06-12 19:04:37
  • Flash在某些多标签浏览器中的“伪沙箱”问题

    2011-01-06 12:37:00
  • VSCode使用ssh密钥免密远程登录服务器的方法

    2022-03-13 22:42:13
  • kafka-python批量发送数据的实例

    2023-04-24 04:40:23
  • Python计算机视觉SIFT尺度不变的图像特征变换

    2022-08-01 00:28:44
  • 浅谈Python pygame绘制机制

    2021-12-03 20:39:02
  • Python通过PIL获取图片主要颜色并和颜色库进行对比的方法

    2022-06-05 13:51:35
  • ASP将Excel数据导入到SQLServer的实现代码

    2011-03-10 10:38:00
  • asp之家 网络编程 m.aspxhome.com