Javascript 我无法在 Chrome 中触发卸载事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12182559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I can't trigger the unload event in Chrome
提问by John Shepard
This code runs fine on Firefox, but I can't make the unload event work on Chromeanymore. Did Chrome stop supporting the unload event?
这段代码在 Firefox 上运行良好,但我不能再让卸载事件在 Chrome 上工作了。Chrome 是否停止支持卸载事件?
This is my code:
这是我的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript" src="../jquery/jquery.js"></script>
<script type="text/javascript">
function pageHidden(evt) { alert("Are you sure 1?"); } //WORKS ON FIREFOX BUT NOT IN CHROME
window.addEventListener("pagehide", pageHidden, false);
window.onunload = function () { alert("Are you sure 2?"); } //TRIGGERS ON LOAD NOT ON UNLOAD
$(window).unload(function () { //WORKS ON FIREFOX BUT NOT IN CHROME
alert("Are you sure 3?");
});
</script>
</head>
<body>
TEST WEBSITE
<a href="http://www.iamawesome.com">external link</a>
</body>
</html>
How can I get the unload event to work in Chrome?
如何让卸载事件在 Chrome 中工作?
Thanks!
谢谢!
ANSWER: Don't test the unload event with alerts ;)
答案:不要用警报测试卸载事件;)
回答by Rocket Hazmat
window.onunload = alert("Are you sure 2?");
This is incorrect. You are setting onunload
to the result of alert
, you need to set it to a function:
这是不正确的。您正在设置onunload
为的结果alert
,您需要将其设置为一个函数:
window.onunload = function(){
alert("Are you sure?");
}
If you want to use jQuery, this will work in all browsers.
如果您想使用 jQuery,这将适用于所有浏览器。
$(window).unload(function () {
alert("Are you sure?");
});
NOTE: It might seemlike it's not working in Chrome, but it is. That's because Chrome blocks alert
s in the onunload
event.
注意:看起来它在 Chrome 中不起作用,但确实如此。那是因为 Chromealert
在onunload
事件中阻止了s 。
回答by Korikulum
Try:
尝试:
window.onbeforeunload = function () {
// code
};
or
或者
window.onpagehide = function () {
// code
};
回答by Bergi
You don't trigger anything - you just execute the expression which you want to add as a handler:
您不会触发任何内容 - 您只需执行要添加为处理程序的表达式:
window.onunload = function(event) {
alert("Are you sure 2?");
};
回答by jeteon
From what I've read it seems Chrome blocks alerts once that event has been triggered. You can run some functions, however, just not anything that interacts with the user it seems.
从我读过的内容来看,一旦触发该事件,Chrome 似乎会阻止警报。您可以运行一些功能,但是,似乎不能与用户交互。
From window.onbeforeunload in Chrome: what is the most recent fix?, it seems, if all you want to do is pop up a confirmation message, you have to do it by returning a string with the message from the function you set as the callback.
来自Chrome 中的 window.onbeforeunload:最近的修复是什么?,看来,如果您只想弹出一条确认消息,则必须通过从您设置为回调的函数中返回一个带有消息的字符串来实现。
window.onbeforeunload = function() {
// Some wrap up code (no alerts, confirms, redirects, etc)
return 'My confirmation messsage';
}
The text "My confirmation message" will then show up in a confirmation dialogue of Chrome's choosing. Firefox documents this behaviour here.
文本“我的确认消息”将显示在 Chrome 选择的确认对话框中。Firefox在此处记录了这种行为。