Chrome浏览器 - 浏览器退出时检测 [英] Chrome - Detect When Browser Exits
问题描述
我想知道是否有可能检测用户是否退出Chrome浏览器?
I am wondering if it is possible to detect whether the user exits the Chrome browser?
编辑 - 对不起,我不是很清楚,所以我会解释我的情况。我在浏览器的本地存储中存储了一些变量。当用户关闭浏览器时,我想删除这些变量中的一部分。
EDIT - Sorry, I wasn't being very clear so I'll explain my situation. I am storing some variables in the browser's localstorage. When the user closes the browser, I want to delete some of these variables.
推荐答案
在卸载窗口之前执行一些JavaScript
您可以钩住窗口的
OnBeforeUnload
事件
Executing some JavaScript before the window is unloaded
You can hook the OnBeforeUnload
event of the window
<script type="text/javascript">
$(window).bind('beforeunload', function() {
if (iWantTo) {
return "Don't leave me!";
}
});
</script>
使用心跳知道用户何时离开
或者创建一个JavaScript定时器,每隔XX秒钟对您的服务器进行一次ping。当ping停止时,您可以假定用户已关闭浏览器或导航。
Using a heartbeat to know when the user has left
Or create a JavaScript timer that pings your sever every XX seconds. When the pings stop, you can assume the user has closed the browser or navigated away.
http://ajaxpatterns.org/archive/Heartbeat.php
这篇关于Chrome浏览器 - 浏览器退出时检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!