关于捕获用户何时点击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:


代码如下:

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

(0)

相关推荐

  • JavaScript实现事件的中断传播和行为阻止方法示例

    事件传播 MicroSoft的设计是当事件在元素上触发时,该事件将接着在该节点的父节点触发,以此类推,事件一直沿着DOM树向上传播,直到到达顶层对象document元素.这种自底向上的事件传播方式称为"事件冒泡",也就是事件传播. 如何中断事件的传播? stopPropagation() w3c取消冒泡 cancleBubble = true IE取消冒泡 取消事件默认效果: returnValue = false IE 取消事件效果 defaultPrevent() w3c取消事件效

  • JavaScript中使用stopPropagation函数停止事件传播例子

    JS中的事件默认是冒泡方式,逐层往上传播,可以通过stopPropagation()函数停止事件在DOM层次中的传播.如以下例子: HTML代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>stopPropagation()使用 - 琼台博客</title> </head> <body> <button&

  • Javascript Event(事件)的传播与冒泡

    特性说明和原理图: 标准浏览器和Ie9+浏览器都支持事件的冒泡和捕获,而IE8-浏览器只支持冒泡 标准和Ie9+浏览器用stopPropagation()或cancelBubble阻止事件传播,而ie8-用e.cancelBubble属性来阻冒泡,注意ie9不支持cancelBubble属性(设置后不生效),但chrome.safari.opera.firefox都支持cancelBubble属性. Ie8-用attachEvent为dom元素添加一个事件,但必须在事件名前加上on,此类事件只能

  • js如何取消事件冒泡

    复制代码 代码如下: function stopBubble(e) { //如果传入了对象,那么就是非IE浏览器,才用W3C标准方法 if (e || e.stopPropagation) { e.stopPropagation(); } else { //才用IE的停止事件冒泡的方法 window.event.CancelBubble = true; } }

  • 阻止事件(取消浏览器对事件的默认行为并阻止其传播)

    取消浏览器对事件的默认行为(响应)(比如<a>标签的跳转等)并停止事件的继续传播. 实现代码 复制代码 代码如下: function stopEvent (evt) { var evt = evt || window.event; if (evt.preventDefault) { evt.preventDefault(); evt.stopPropagation(); } else { evt.returnValue = false; evt.cancelBubble = true; } }

  • jquery取消事件冒泡的三种方法(推荐)

    1.通过返回false来取消默认的行为并阻止事件起泡. jQuery 代码: $("form").bind( "submit", function() { return false; } ); 2.通过使用 preventDefault() 方法只取消默认的行为. jQuery 代码: $("form").bind( "submit", function(event){ event.preventDefault(); } );

  • JS传播事件、取消事件默认行为、阻止事件传播详解

    1.事件处理程序的返回值  通常情况下,返回值false就是告诉浏览器不要执行这个事件相关的默认操作.例如,表单提交按钮的onclick事件处理程序能通过返回false阻止浏览器提交表单,再如a标签的onclick事件处理程序通过返回false阻止跳转href页面.类似地,如果用户输入不合适的字符,输入域上的onkeypress事件处理程序能通过返回false来过滤键盘输入.        事件处理程序的返回值只对通过属性注册的处理程序才有意义. 2.调用顺序 文档元素或其他对象可以为指定事件类

  • flex中event.preventDefault()方法取消事件的默认行为

    先看一个例子: 复制代码 代码如下: <mx:DataGrid id= "songList" dataProvider= "{songDB}" width= "100%" height= "100%" editable= "true" itemEditEnd="itemEditEndHandler(event)" itemEditBeginning= "itemEditB

  • 关于捕获用户何时点击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 glo

  • window.onbeforeunload方法在IE下无法正常工作的解决办法

    事件的起因是由于在工作中有客户反映,常常会有用户在浏览网页的过程中订购了商品,但是由于用户一下子打开的窗口过多,又或者在敲打键盘时,错误地按到了F5键,导致页面刷新或者不正常关闭,而这时在该网页上所做的一切操作的信息都丢失了,如果我们可以提供一个在客户信息未处理完成时的提示那该多好啊,下面的代码可以做到不管用户是点击了关闭,或者是在任务栏关闭.点击后退.刷新.按F5键,都可以检测到用户即将离开的消息. 复制代码 代码如下: <script type="text/javascript&quo

  • WPF中button按钮同时点击多次触发click解决方法

    解决WPF中button按钮同时点击多次触发click的方法,供大家参考,具体内容如下 DateTime lastClick = DateTime.Now; object obj = new object(); int i = 0; private void Button_Click(object sender, RoutedEventArgs e) { this.IsEnabled = false; var t = (DateTime.Now - lastClick).TotalMillise

  • 浅谈window.onbeforeunload() 事件调用ajax

    经常有这样的需求,就是在离开某个web页面时,用户不一定点注销,这样会导致会话不能及时销毁.为实现用户离开页面时,自动注销功能,需要在web页面的onbeforeunload事件处理函数中发送注销命令.这个地方大多用Ajax实现.有时还涉及到跨域访问的问题.这个地方就存在浏览器的兼容性问题. 浏览器在处理这个需求时的不兼容性有如下两点: 1.处理Ajax时的不兼容性,这里使用jQuery来解决. 2.在发送Ajax请求时的不兼容性 主要代码如下: function logout() { var

  • 当用户退出点击浏览器后退仍可回到原来页面的解决方案

    解决方案1:禁用缓存,前一次使用的方法,在电脑上各浏览器都没问题,但在ipad.安卓手机上仍有问题 解决方案2:禁用浏览器后退键 javascript: window.history.forward(1); 结果和方案一一样的结果,pad上没效果 解决方案3:Response.Write("<script>window.location.replace('login.aspx')</script>");仍旧可以后退,感觉还不如1.2,但是在前台加个onclick

  • 基于vue循环列表时点击跳转页面的方法

    1.在data数组里边添加id(说明:我的是虚拟数据) 2.在点击事件上传入id参数,如下: 3.在methods里边添加点击跳转的方法,不要忘记在function后边的括号内传入id,然后判断如果id==1,就跳转那个页面,id==2跳转那个页面. 至此跳转完成. 附加: 点击返回上一页方法: window.history.go(-1);就是返回上一页.(不要忘记在标签上添加click点击事件) returnS:function () { window.history.go(-1); } 以上

  • Element的穿梭框数据量大时点击全选卡顿的解决方案

    目录 方案一:复制EUI的transfer组件,然后进行修改,再引入项目目录 方案二:分页操作 分析 方案 现象:我们渲染了9999条数据,由于transfer组件会一次性渲染所有数据,所以一次性渲染这么多,卡个几十秒很正常好吧.所以懒加载或者分页是基本操作,方案二是分页操作. 懒加载的方式可以用EUI的无限滚动:https://element.eleme.cn/ 即便我们做了懒加载之后,点击全选依旧是卡顿6秒以上,所以方案一解决的是:即便做了懒加载或者分页操作后,用户点击分页,依旧会卡顿几秒的

  • jQuery实现点击DIV同时点击CheckBox,并为DIV上背景色的实例

    废话不多说,直接上代码吧 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>JqueryTest</title> <script src="js/jquery-1.8.3.min.js"></script> <script> $(document).r

  • 用js判断页面刷新或关闭的方法(onbeforeunload与onunload事件)

    Onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过window.onunload来指定或者在<body>里指定.区别在于onbeforeunload在onunload之前执行,它还可以阻止onunload的执行. Onbeforeunload也是在页面刷新或关闭时调用,Onbeforeunload是正要去服务器读取新的页面时调用,此时还没开始读取:而onunload则已经从服务器上读到了需要加载的新的页面,在即将替换掉当前页面时调

  • 浅谈javascript中onbeforeunload与onunload事件

    在最近的项目中,需要做到一个时间,就是用户离开页面的时候,我需要缓存页面其中一部分的内容,但是我不需要用户刷新的时候也缓存,我只希望在我用户离开的时候 执行这个函数.百度之,有onbeforeunload与onunload这两个事件,但是onbeforeunload在用户刷新的时候也会执行.搞得我弄的挺久的,所以想在这里做一个小小的总结 onbeforeunload与onunload事件 onbeforeunload定义和用法 onbeforeunload 事件在即将离开当前页面(刷新或关闭)时

随机推荐