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


猜你喜欢
- 本文实例讲述了python生成IP段的方法。分享给大家供大家参考。具体实现方法如下:#!/usr/local/bin/python#-*-
- 前言今天制作的这一款能在B站能指定直播间、自动发弹幕的功能的脚本,因为没做那么多的功能,所以代码很简单,适合刚入门的同学学习先打开一个直播间
- Python脚本常见参数获取和处理平常写 python 脚本时会有一些从命令行获取参数的需求,这篇文章记录下常见的参数获取和处理方式。1.
- Python 编程中可以使用 PyMysql 进行数据库的连接及诸如查询/插入/更新等操作,但是每次连接 MySQL 数据库请求时,都是独立
- 本文实例为大家分享了PHP变量传值赋值和引用赋值变量销毁的具体代码,供大家参考,具体内容如下<?php $a = 100
- python标记语句块使用方法,python语言和其它的编程语言有着显著的区别,那就是python对格式的要求非常苛刻,好处就是书写上简易命
- 首先要声明一下:一般情况下,修改MySQL密码,授权,是需要有mysql里的root权限的。 注:本操作是在WIN命令提示符下,phpMyA
- 在本文中,此示例标准蓝图的存储过程命名方法只适用于SQL内部,假如你正在创建一个新的存储过程,或是发现一个没有按照这个标准构造的存储过程,即
- 本文实例讲述了JavaScript数据库TaffyDB用法。分享给大家供大家参考。具体如下:TaffyDB 是一个免费开源的 JavaScr
- 一、需求来源:如果用户在文本框中填了一段<script>alert(xxx);</script>代码,然后我们还保存
- 前言本文题目中虽然写有vue和react,但是并非vue和react相关知识,而是最基本的html5和css3的一些知识,之所以写vue,是
- 在接口测试学习过程中,遇到了利用requests库进行文件下载和上传的问题。同样,在真正的测试过程中,我们不可避免的会遇到上传和下载的测试。
- 今天发现个好东西啊,叫片刻抠图,是一个在线对图片自动抠图去除背景的网站。只要上传图片,就可以自动把背景去掉把目标对象抠出来。不管是动物、汽车
- Python的线程操作在旧版本中使用的是thread模块,在Python27和Python3中引入了threading模块,同时thread
- 当一张的数据达到几百万时,你查询一次所花的时间会变多,如果有联合查询的话,我想有可能会死在那儿了。分表的目的就在于此,减小数据库的负担,缩短
- 通常,当我们在 Vue 中创建组件时,它们出现在我们期望的 DOM 结构中。但是,有时我们并不希望如此。一个很好的例子就是模态框&m
- 因为有把python程序打包成exe的需求,所以,有了如下的代码import timeclass LoopOver(Exception):
- 本文实例讲述了Python中subprocess模块用法。分享给大家供大家参考。具体如下:执行命令:>>> subproc
- 本文实例讲述了PHP获取二叉树镜像的方法。分享给大家供大家参考,具体如下:问题操作给定的二叉树,将其变换为源二叉树的镜像。解决思路翻转二叉树
- 方式一、使用localStorage在数据存储1、要在浏览器刷新的时候重新存储起来if (window.localStorage.getIt