关闭窗口后如何注销我的应用程序

关闭窗口后如何注销我的应用程序

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

问题描述

在我的聊天应用程序中,我具有注销按钮,并且可以正常工作.

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

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

08-27 08:40