admin 管理员组文章数量: 1184232
In my chat application I need to get confirmation from user, when my application closes.
So I used the window.onbeforeunload for confirmation alert and window.onunload for
logout().
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.
My JavaScript code will be:
// 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,
<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>
In my chat application I need to get confirmation from user, when my application closes.
So I used the window.onbeforeunload for confirmation alert and window.onunload for
logout().
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.
My JavaScript code will be:
// 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,
<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>
Share
Improve this question
edited Dec 14, 2020 at 10:49
Brian Tompsett - 汤莱恩
5,87572 gold badges61 silver badges133 bronze badges
asked Feb 1, 2013 at 11:10
Human BeingHuman Being
8,38728 gold badges97 silver badges140 bronze badges
11
|
Show 6 more comments
6 Answers
Reset to default 107 +50Unfortunately, the methods you are using are unsupported in those browsers. To support my answer (this unsupportive behaviour) I have given links below.
onbeforeunload and onunload not working in opera... to support this
onbeforeunload in Opera
http://www.zachleat.com/web/dont-let-the-door-hit-you-onunload-and-onbeforeunload/
Though the onunload event doesn't work completely, you can use onunload to show a warning if a user clicks a link to navigate away from a page with an unsaved form.
onunload not working in safari... to support this
https://www.webkit.org/blog/516/webkit-page-cache-ii-the-unload-event/
You could rather try using the pagehide event in the safari browser in lieu of onunload.
onunload not working in firefox... to support this
https://bugzilla.mozilla.org/show_bug.cgi?id=681636
They are yet to come up with a solution in FF too.
Here is the working solution for ie, firefox and 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;
});
I was able to get it to work in IE and FF with jQuery's:
$(window).bind('beforeunload', function(){
});
instead of: unload, onunload, or onbeforeunload
I got the solution for onunload in all browsers except Opera by changing the Ajax asynchronous request into synchronous request.
xmlhttp.open("POST","LogoutAction",false);
It works well for all browsers except Opera.
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.
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.
'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;
};
Firefox simply does not show custom onbeforeunload messages. Mozilla say they are protecing end users from malicious sites that might show misleading text.
本文标签:
版权声明:本文标题:javascript - window.onbeforeunload and window.onunload is not working in Firefox, Safari, Opera? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1736915121a1763160.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
onbeforeunloadand its support ofonunloadin incomplete. – Marcel Korpel Commented Feb 1, 2013 at 12:03