检测窗口关闭 [英] Detect window closed
本文介绍了检测窗口关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个聊天客户端,该客户端使用数据库来检查会话的状态(0等待,1运行,2关闭).
I'm creating a chat client which uses a database to check the status of a session 0-waiting, 1-running, 2-closed.
我有一个关闭按钮,该按钮会将状态更改为2,但我想知道如何仅按x来检测浏览器是否关闭?
I have a close button which will change the status to 2 but I was wondering, how do I detect if the browser is closed just by pressing x?
主要原因是当一个参与者关闭浏览器时,我不希望会话显示为正在运行.
The main reason for this is I don't want the session showing up as running when one of the participants has closed their browser.
推荐答案
使用unload
和onbeforeunload
事件:
window.onunload = window.onbeforeunload = function (){...};
最好注册两个事件,以确保将触发回调.
It's better to register to both of the events to be sure the callback will fire.
这篇关于检测窗口关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文