window.onbeforeunload 和 window.onunload 在 Firefox、Safari、Opera 中不起作用? [英] window.onbeforeunload and window.onunload is not working in Firefox, Safari, Opera?

查看:28
本文介绍了window.onbeforeunload 和 window.onunload 在 Firefox、Safari、Opera 中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的聊天应用程序中,当我的应用程序关闭时,我需要得到用户的确认.

In my chat application I need to get confirmation from user, when my application closes.

所以我使用 window.onbeforeunload 作为确认警报和 window.onunloadlogout().

So I used the window.onbeforeunload for confirmation alert and window.onunload for logout().

  1. 但这两个功能都可以在 IE 和 Chrome 中使用.(应用程序工作正常)

  1. But both functions are working in IE and Chrome. (Application works fine)

window.onbeforeunload 在 Opera 中不起作用,我的消息不会在 Firefox 中显示.

window.onbeforeunload not working in Opera and my message will not get displayed in Firefox.

window.onunload 在 Safari、Opera 和 Firefox 中不起作用.

window.onunload not working in Safari, Opera and Firefox.

我的 JavaScript 代码将是:

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();
};

我也用 JQuery 尝试了同样的功能,

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>

推荐答案

通过将 Ajax 异步请求改为同步请求,我得到了在除 Opera 之外的所有浏览器中 onunload 的解决方案.

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);

它适用于除 Opera 之外的所有浏览器.

It works well for all browsers except Opera.

这篇关于window.onbeforeunload 和 window.onunload 在 Firefox、Safari、Opera 中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆