在我的聊天应用程序中,当我的应用程序关闭时,我需要得到用户的确认。
所以我用了window.onbeforeunload
用于确认警报和window.onunload
for
logout()
.
-
但这两个功能都可以在 IE 和 Chrome 中使用。 (应用程序运行良好)
-
window.onbeforeunload
在 Opera 中不起作用,我的消息也不会在 Firefox 中显示。
-
window.onunload
不适用于 Safari、Opera 和 Firefox。
我的 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();
};
我也尝试使用 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>
不幸的是,这些浏览器不支持您正在使用的方法。为了支持我的回答(这种不支持的行为),我在下面给出了链接。
onbeforeunload
and onunload
不在opera
...支持这个
Opera 中的 onbeforeunload https://stackoverflow.com/questions/390260/onbeforeunload-in-opera
http://www.zachleat.com/web/dont-let-the-door-hit-you-onunload-and-onbeforeunload/ http://www.zachleat.com/web/dont-let-the-door-hit-you-onunload-and-onbeforeunload/
虽然onunload
事件不能完全工作,你可以使用onunload
如果用户单击链接离开包含未保存表单的页面,则显示警告。
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/
你宁愿尝试使用pagehide
Safari 浏览器中的事件代替onunload
.
onunload
不在firefox
...支持这个
https://bugzilla.mozilla.org/show_bug.cgi?id=681636 https://bugzilla.mozilla.org/show_bug.cgi?id=681636
他们还没有在 FF 中找到解决方案。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)