Javascript window.onbeforeunload 和 window.onunload 在 Firefox 、 Safari 、 Opera 中不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14645011/
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.onbeforeunload and window.onunload is not working in Firefox , Safari , Opera?
提问by Human Being
In my chat application I need to get confirmation from user , when my application closes.
在我的聊天应用程序中,当我的应用程序关闭时,我需要得到用户的确认。
So I used the window.onbeforeunloadfor confirmation alert and window.onunloadfor
logout().
所以我使用了window.onbeforeunload确认警报和window.onunload注销()。
But both functions are working in IE and Chrome.( Application works fine)
window.onbeforeunloadnot working in Opera and my message will not get displayed in Firefox.window.onunloadnot working in Safari , Opera and Firefox.
但是这两个功能都在 IE 和 Chrome 中工作。(应用程序工作正常)
window.onbeforeunload在 Opera 中不起作用,我的消息不会在 Firefox 中显示。window.onunload不适用于 Safari 、 Opera 和 Firefox。
My javaScript code will be ,
我的 javaScript 代码将是,
// Used for confirmation , to closing the window
window.onbeforeunload = function () {
return "Are you sure want to LOGOUT the session ?";
};
// Used to logout the session , when browser window was closed
window.onunload = function () {
if((sessionId != null)&&(sessionId!="null")&& (sessionId != ""))
logout();
};
I also tried the same function with JQuery,
我也用 JQuery 尝试了同样的功能,
<script type="text/javascript">
$(window).on('beforeunload', function() {
return 'Are you sure want to LOGOUT the session ?';
});
$(window).unload(function() {
if ((sessionId != null) && (sessionId != "null") && (sessionId != "")) {
logout();
}
});
</script>
采纳答案by Human Being
I got the solution for onunloadin all browsers except Opera by changing the Ajax asynchronous request into synchronous request.
onunload通过将 Ajax 异步请求更改为同步请求,我在除 Opera 之外的所有浏览器中获得了解决方案。
xmlhttp.open("POST","LogoutAction",false);
It works well for all browsers except Opera.
它适用于除Opera之外的所有浏览器。
回答by Freakyuser
Unfortunately, the methods you are using are unsupported in those browsers. To support my answer (this unsupportive behaviour) I have given links below.
不幸的是,这些浏览器不支持您使用的方法。为了支持我的回答(这种不支持的行为),我在下面提供了链接。
onbeforeunloadand onunloadnot working in opera... to support this
onbeforeunload而onunload不是在opera......支持这一点
onbeforeunload in Opera
http://www.zachleat.com/web/dont-let-the-door-hit-you-onunload-and-onbeforeunload/
Opera 中的 onbeforeunload
http://www.zachleat.com/web/dont-let-the-door-hit-you-onunload-and-onbeforeunload/
Though the onunloadevent doesn't work completely, you can use onunloadto show a warning if a user clicks a link to navigate away from a page with an unsaved form.
尽管该onunload事件不能完全起作用,但onunload如果用户单击链接以离开带有未保存表单的页面,您可以使用它来显示警告。
onunloadnot working in safari... to support this
onunload不工作safari......支持这一点
https://www.webkit.org/blog/516/webkit-page-cache-ii-the-unload-event/
https://www.webkit.org/blog/516/webkit-page-cache-ii-the-unload-event/
You could rather try using the pagehideevent in the safari browser in lieu of onunload.
您可以尝试pagehide在 safari 浏览器中使用该事件代替onunload.
onunloadnot working in firefox... to support this
onunload不工作firefox......支持这一点
They are yet to come up with a solution in FF too
他们也还没有在 FF 中提出解决方案
Wish you good luck cheers.
祝你好运加油。
回答by faisale
Here is the working solution for ie, firefox and chrome:
这是 ie,firefox 和 chrome 的工作解决方案:
var myEvent = window.attachEvent || window.addEventListener;
var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; /// make IE7, IE8 compitable
myEvent(chkevent, function(e) { // For >=IE7, Chrome, Firefox
var confirmationMessage = 'Are you sure to leave the page?'; // a space
(e || window.event).returnValue = confirmationMessage;
return confirmationMessage;
});
回答by nathan hayfield
I was able to get it to work in IE and FF with jQuery's:
我能够使用 jQuery 让它在 IE 和 FF 中工作:
$(window).bind('beforeunload', function(){
});
instead of: unload, onunload, or onbeforeunload
代替:卸载、卸载或卸载
回答by leeway
The onunload event is not called in all browsers. Worse, you cannot check the return value of onbeforeunload event. That prevents us from actually preforming a logout function.
并非在所有浏览器中都调用 onunload 事件。更糟糕的是,您无法检查 onbeforeunload 事件的返回值。这阻止了我们实际执行注销功能。
However, you can hack around this.
但是,您可以解决此问题。
Call logout first thing in the onbeforeunload event. then prompt the user. If the user cancels their logout, automatically login them back in, by using the onfocus event. Kinda backwards, but I think it should work.
在 onbeforeunload 事件中首先调用注销。然后提示用户。如果用户取消他们的注销,则使用 onfocus 事件自动将他们重新登录。有点倒退,但我认为它应该有效。
'use strict';
var reconnect = false;
window.onfocus = function () {
if (reconnect) {
reconnect = false;
alert("Perform an auto-login here!");
}
};
window.onbeforeunload = function () {
//logout();
var msg = "Are you sure you want to leave?";
reconnect = true;
return msg;
};
回答by anoldermark
Firefox simply does not show custom onbeforeunload messages. Mozilla say they are protecing end users from malicious sites that might show misleading text.
Firefox 根本不显示自定义的 onbeforeunload 消息。Mozilla 表示,他们正在保护最终用户免受可能显示误导性文本的恶意站点的侵害。

