Detect browser or tab closing(检测浏览器或标签页关闭)
问题描述
是否有任何跨浏览器 JavaScript/jQuery 代码来检测浏览器或浏览器选项卡是否正在关闭,但不是由于单击了链接?
Is there any cross-browser JavaScript/jQuery code to detect if the browser or a browser tab is being closed, but not due to a link being clicked?
推荐答案
如果我理解正确,您想知道选项卡/窗口何时有效关闭.好吧,AFAIK 在 Javascript
中检测这种东西的唯一方法是 onunload
&onbeforeunload
事件.
If I get you correctly, you want to know when a tab/window is effectively closed. Well, AFAIK the only way in Javascript
to detect that kind of stuffs are onunload
& onbeforeunload
events.
不幸的是(或幸运的是?),当您通过 link
或浏览器的后退按钮离开网站时,也会触发这些事件.所以这是我能给出的最好答案,我认为你不能在Javascript中原生检测到纯 close
.如果我在这里错了,请纠正我.
Unfortunately (or fortunately?), those events are also fired when you leave a site over a link
or your browsers back button. So this is the best answer I can give, I don't think you can natively detect a pure close
in Javascript. Correct me if I'm wrong here.
这篇关于检测浏览器或标签页关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:检测浏览器或标签页关闭


基础教程推荐
- 如何添加到目前为止的天数? 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01