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


Posted in Javascript onMarch 06, 2011

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
Javascript 相关文章推荐
javascript:以前写的xmlhttp池,代码
May 18 Javascript
javascript中$(function() {});写与不写有哪些区别
Aug 10 Javascript
深入浅析JavaScript中with语句的理解
May 12 Javascript
微信小程序  action-sheet详解及实例代码
Nov 09 Javascript
Vue关于数据绑定出错解决办法
May 15 Javascript
原生JS与jQuery编写简单选项卡
Oct 30 jQuery
Vue下的国际化处理方法
Dec 18 Javascript
vue项目打包后打开页面空白解决办法
Jun 29 Javascript
记录一次开发微信网页分享的步骤
May 07 Javascript
vuex 实现getter值赋值给vue组件里的data示例
Nov 05 Javascript
JS检测浏览器开发者工具是否打开的方法详解
Oct 02 Javascript
JavaScript实现网页跨年倒计时
Dec 02 Javascript
js中将具有数字属性名的对象转换为数组
Mar 06 #Javascript
js 优化次数过多的循环 考虑到性能问题
Mar 05 #Javascript
淘宝搜索框效果实现分析
Mar 05 #Javascript
再论Javascript下字符串连接的性能
Mar 05 #Javascript
再论Javascript的类继承
Mar 05 #Javascript
Array的push与unshift方法性能比较分析
Mar 05 #Javascript
js定义对象或数组直接量时各浏览器对多余逗号的处理(json)
Mar 05 #Javascript
You might like
ajax取消挂起请求的处理方法
2013/03/18 PHP
php在线解压ZIP文件的方法
2014/12/30 PHP
php注册登录系统简化版
2020/12/28 PHP
JavaScript 浏览器验证代码(来自discuz)
2010/07/17 Javascript
jquery如何判断表格同一列不同行input数据是否重复
2014/05/14 Javascript
Node.js中使用mongoskin操作mongoDB实例
2014/09/28 Javascript
js中style.display=&quot;&quot;无效的解决方法
2014/10/30 Javascript
JavaScript学习笔记之DOM基础 2.4
2015/08/14 Javascript
jQuery实现的自定义弹出层效果实例详解
2016/09/04 Javascript
Vue 2.0的数据依赖实现原理代码简析
2017/07/10 Javascript
JS自定义滚动条效果简单实现代码
2020/10/27 Javascript
[04:05]TI9战队采访 - Natus Vincere
2019/08/22 DOTA
在Linux上安装Python的Flask框架和创建第一个app实例的教程
2015/03/30 Python
使用Python写一个贪吃蛇游戏实例代码
2017/08/21 Python
详解如何将python3.6软件的py文件打包成exe程序
2018/10/09 Python
python3 中文乱码与默认编码格式设定方法
2018/10/31 Python
详解如何在Apache中运行Python WSGI应用
2019/01/02 Python
Python利用lxml模块爬取豆瓣读书排行榜的方法与分析
2019/04/15 Python
python2使用bs4爬取腾讯社招过程解析
2019/08/14 Python
python 实现快速生成连续、随机字母列表
2019/11/28 Python
HTML5 embed标签定义和用法详解
2014/05/09 HTML / CSS
英国外籍人士的在线超市:British Corner Shop
2019/06/03 全球购物
俄罗斯领先的移动和数字设备在线商店:Svyaznoy.ru
2020/12/21 全球购物
Wiggle澳大利亚:自行车、跑步、游泳商店
2020/11/07 全球购物
华美博弈C/VC工程师笔试试题
2012/07/16 面试题
幼儿园中秋节活动方案2013
2014/01/29 职场文书
教学个人的自我评价分享
2014/02/16 职场文书
评析教师个人的自我评价
2014/02/19 职场文书
行政求职信
2014/07/04 职场文书
关于保护环境的建议书
2014/08/26 职场文书
户籍证明书标准模板
2014/09/10 职场文书
奉献家乡演讲稿
2014/09/16 职场文书
2014年人民警察入党思想汇报
2014/10/12 职场文书
反邪教警示教育活动总结
2015/05/09 职场文书
python如何进行基准测试
2021/04/26 Python
Python使用psutil库对系统数据进行采集监控的方法
2021/08/23 Python