关闭窗口时如何注销我的应用程序? [英] How to logout my application when I closed the window?

查看:36
本文介绍了关闭窗口时如何注销我的应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的聊天应用程序中,我有退出按钮,它工作正常.

In my chat application i am having the logout button and it works fine.

现在我需要在关闭浏览器窗口时注销应用程序..我怎样才能做到这一点...

Now I need to logout the application when I closed the browser window also..How can I achieve this...

提前致谢...

推荐答案

将您的注销代码添加到 onunload 事件.

Add your logout code to the on onunload event.

window.onunload = function () {
    //logout code here...
}

在 JQuery 中,您可以使用 .unload() 函数.请记住,您没有太多时间,因此您可以发送 Ajax 请求,但结果可能无法到达客户端.

In JQuery you can use the .unload() function. Remember that you don't have much time so you may send the Ajax request but the result may not reach the client.

另一个技巧是打开一个新的小窗口并在那里处理注销.

Another trick is to open a small new window and handle the logout there.

window.open("logout url","log out","height=10,width=10,location=no,menubar=no,status=no,titlebar=no,toolbar=no",true);

如果您想禁用关闭窗口(或至少警告用户),您可以使用以下代码:

If you want to disable closing the window (or at least warn the user), you can use this code:

window.onbeforeunload = function(event) {
    //if you return anything but null, it will warn the user.
    //optionally you can return a string which most browsers show to the user as the warning message.
    return true;
}

另一个技巧是每隔几秒钟就不断地ping客户端.如果没有回复,则假设用户已关闭窗口、浏览器崩溃或网络问题导致聊天会话结束.在客户端,如果你没有收到这个ping包,你可以假设网络连接或服务器有问题,你可以显示注销警告(并可选择让用户重新登录).

Another trick is to keep pinging the client every few seconds. If no reply comes back, assume the user has closed the window, browser has crashed or there is a network issue that ended the chat session anyway. On the client side, if you don't receive this ping package, you can assume that network connection or server has a problem and you can show the logout warning (and optionally let the user login again).

这篇关于关闭窗口时如何注销我的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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