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

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

时间: 2024-12-28 08:11:42

关于捕获用户何时点击window.onbeforeunload的取消事件_javascript技巧的相关文章

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

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

JavaScript判断用户是否对表单进行了修改的方法_javascript技巧

本文实例讲述了JavaScript判断用户是否对表单进行了修改的方法.分享给大家供大家参考.具体分析如下: 这段JS代码可以判断出用户是否对表单内容进行了修改,如果修改了表单,并退出浏览器,则会提醒用户是否要保存表单的内容,是非常有用的代码. function formIsDirty(form) { for (var i = 0; i < form.elements.length; i++) { var element = form.elements[i]; var type = element

判断用户的在线状态 onbeforeunload事件_javascript技巧

获得用户登陆状态不用说了,判断离开的话就有一点问题了,如果说用户都是按照设计者的规定触发退出事件离开的话那就没什么难度了,但是用户的离开方式多种多样,怎么在用户非法离开的时候即时的判断离开呢?最常见的非法离开就是关闭浏览器了. 复制代码 代码如下: <BODY onbeforeunload="body_onUnload()"> <script> window.onbeforeunload = function() { if (window.event.clien

JavaScript 捕获窗口关闭事件_javascript技巧

1.用javascript重新定义 window.onbeforeunload() 事件 在javascript里定义一个函数即可 function window.onbeforeunload() { alert("关闭窗口")} alert()事件将会在关闭窗口前执行,你也可以用户决定是否关闭窗口 function window.onbeforeunload() { if (event.clientX>document.body.clientWidth && e

js实现鼠标点击左上角滑动菜单效果代码_javascript技巧

本文实例讲述了js实现鼠标点击左上角滑动菜单效果代码.分享给大家供大家参考.具体如下: 这里需要鼠标点击激活的网页左上角菜单,不点击不会滑出来,可以改造成二级菜单. 运行效果截图如下: 在线演示地址如下: http://demo.jb51.net/js/2015/js-click-hd-show-menu-style-codes/ 具体代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &quo

JavaScript中Window对象的属性及事件_javascript技巧

Window 对象 Window 对象表示浏览器中打开的窗口. 如果文档包含框架(frame 或 iframe 标签),浏览器会为 HTML 文档创建一个 window 对象,并为每个框架创建一个额外的 window 对象. 注释:没有应用于 window 对象的公开标准,不过所有浏览器都支持该对象. windows对象属性 属性 描述 closed 返回窗口是否已被关闭. defaultStatus 设置或返回窗口状态栏中的默认文本. document 对 Document 对象的只读引用.请

js简单的点击返回顶部效果实现方法_javascript技巧

本文实例讲述了js简单的点击返回顶部效果实现方法.分享给大家供大家参考.具体分析如下: 当页面特别长的时候,用户想回到页面顶部,必须得滚动好几次滚动键才能回到顶部,如果在页面右下角有个"返回顶部"的按钮,用户点击一下,就可以回到顶部,对于用户来说,是一个比较好的体验. 实现原理:当页面加载的时候,把元素定位到页面的右下角,当页面滚动时,元素一直位于右下角,当用户点击的时候,页面回到顶部. 要点一:document.documentElement.clientWidth || docum

js window.event对象详尽解析_javascript技巧

event代表事件的状态,例如触发event对象的元素.鼠标的位置及状态.按下的键等等. event对象只在事件发生的过程中才有效. event的某些属性只对特定的事件有意义.比如,fromElement 和 toElement 属性只对 onmouseover 和 onmouseout 事件有意义. 例子 下面的例子检查鼠标是否在链接上单击,并且,如果shift键被按下,就取消链接的跳转. <HTML> <HEAD><TITLE>Cancels Links</T

JavaScript 模拟用户单击事件_javascript技巧

一开始,我想到了用jQuery的click()事件来触发超链接的单击事件(与trigger("click")一样的效果).结果发现不如人意. 实例如下: 效果图 IE: FireFox:   复制代码 代码如下: <h3>请单击"Click Me".测试提交按钮与超链接是否也被单击了.</h3>     <button id="btn">Click Me</button>     <form