jQuery $(window).unload 没有触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9385778/
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
$(window).unload is not firing
提问by Sergio Romero
I want to execute an action method when the user is abandoning a particular page using jQuery.
当用户放弃使用 jQuery 的特定页面时,我想执行一个操作方法。
The page has the following code:
该页面具有以下代码:
<script type="text/javascript">
$(window).unload(function () {
alert("Handler for .unload() was called.");
});
</script>
When I navigate away from the page I never see the expected alert.
当我离开页面时,我从未看到预期的警报。
回答by Darin Dimitrov
Actually some browsers such as Google Chrome might block if you attempt to alert
in a window unload
. As a user I like this feature. Alerting everytime you try to navigate away from a page sucks:
实际上,如果您尝试alert
在窗口中访问,某些浏览器(例如 Google Chrome)可能会阻止unload
。作为用户,我喜欢这个功能。每次尝试离开页面时都发出警报很糟糕:
Replace the alert with a console.log
or something else less intrusive to the user and the event will be happily called.
用 aconsole.log
或其他对用户不那么干扰的东西替换警报,该事件将被愉快地调用。
You might also want to checkout the onbeforeunloadevent.
您可能还想查看onbeforeunload事件。
回答by bhelm
jquery .on('unload',..); was not reliable working for me. i switched over to use beforeunload. just make sure you are not returning anything, or the user will get a "are you sure to leave the page"-popup.
jquery .on('卸载',..); 对我来说不可靠。我切换到使用 beforeunload。只要确保您没有返回任何内容,否则用户将收到“您确定要离开页面吗”的弹出窗口。
<script type='text/javascript'>
$(window).on('beforeunload', function(){
console.log("beforeUnload event!");
});
</script>
回答by Benny Margalit
as bhelm said
beforeunload works for me as well.
return false on this event will invoke the browser default
正如 bhelm 所说
beforeunload 也适用于我。
在此事件上返回 false 将调用浏览器默认值
are you sure you want to leave this page?
您确定要离开此页面吗?
$(window).on('beforeunload', function ()
{
return false;
});
回答by Mohsin Shoukat
if you want to alert user that you leaving this page then if this
如果你想提醒用户你离开这个页面,那么如果这
/* $(window).unload(function()
{
//alert("you leaving this page");
console.log("you leaving this page");
}); */
function not work for you then replace your code with on("beforeunload",function) like this
函数对你不起作用,然后用 on("beforeunload",function) 替换你的代码,就像这样
$(window).on("beforeunload", function()
{
alert("you leaving this page");
console.log("you leaving this page");
});
this work for me ! you can see the output in console log you leaving this page
这对我有用!您可以在离开此页面的控制台日志中看到输出
回答by Jonas Lundman
In many projects of mine, the mentioned methods here are instable. The only thing that worksfor me is to bind the event as original attribute on the body element.
在我的很多项目中,这里提到的方法是不稳定的。那唯一的工作对我来说是绑定事件为主体元素的原始属性。
<body onunload="my_function_unload()">
jQuery method:
jQuery方法:
$('body').attr('onunload', 'my_function_unload()');
From an iframe:
从 iframe:
<body onunload="window.parent.my_function_unload()">
jQuery method:
jQuery方法:
$('<iframe />').load(function(){
$body = $(this).contents().find('body');
$body.attr('onunload', 'window.parent.my_function_unload()');
}
Also, important, no arguments in the attribute, and the function must be in the global window scope, otherwise nothing happens.
另外,important,属性中没有参数,并且函数必须在全局窗口范围内,否则什么都不会发生。
For example, common mistake If your my_function_unload()
are wrapped inside a ;( function( $ ) {...
OR $(document).ready(function(){...
AS my_function_unload()
must be outside that private scope. And dont forget to use jQuery
instead of $
prefix then.
例如,常见错误如果您my_function_unload()
被包裹在;( function( $ ) {...
OR 内,则$(document).ready(function(){...
ASmy_function_unload()
必须在该私有范围之外。并且不要忘记使用jQuery
而不是$
前缀。
回答by Kshitiz
window.onbeforeunload=navigationError;
var dont_confirm_leave = 0; var leave_message = 'You sure you want to leave?';
function navigationError(e)
{
if(dont_confirm_leave!==1)
{
if(!e) e = window.event;
//e.cancelBubble is supported by IE - this will kill the bubbling process.
e.cancelBubble = true;
e.returnValue = leave_message;
//e.stopPropagation works in Firefox.
if (e.stopPropagation)
{
e.stopPropagation();
e.preventDefault();
}
//return works for Chrome and Safari
return leave_message;
}
}