如何使用jQuery检测浏览器是否关闭 [英] How to detect if browser is closed using jQuery

查看:60
本文介绍了如何使用jQuery检测浏览器是否关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检测是否使用jQuery关闭了浏览器,如果为true,则执行一个事件.我在我的网站上(类似于Facebook)具有聊天功能,该功能可以使用户在导航到网站结构中的其他页面时保持登录状态,但是如果单击它们,则需要将其注销以退出聊天(这样它们就不会在其他用户面前显示在线状态)浏览器的关闭按钮.因此 .unload()将不起作用,因为.unload()将注销功能应用于所有窗口操作,包括导航到网站结构中的另一个页面. .unload()适用于浏览器关闭,但也适用于浏览器的向前和向后导航,因此,我需要一种方法来检测浏览器是否处于关闭状态,并且仅用于检测浏览器是否处于关闭状态.

I need to detect if the browser is being closed using jQuery, and do an event if this is true. I have a chat feature on my website (much like Facebook) that keeps the user logged in as they navigate to other pages within the website structure but needs to log them out of chat (so they do not appear online to others) if they click the browsers close button. So .unload() will not work because .unload() will apply the logout function to all window actions including navigating to another page within the website structure. .unload() works for browser closes, but also works for browser forward and backward navigation, so I need a way to detect if the browser is being closed AND ONLY if the browser if being closed.

推荐答案

无论您使用什么聊天(经过轨道处理的,websocket?),都应该能够检测到会话超时.

Whatever you're using for chat (orbited, websockets?) should be able to detect a session timeout.

这篇关于如何使用jQuery检测浏览器是否关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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