如何在浏览器关闭时触发jQuery
有谁知道可靠的方法监听 javascript/ jQuery 中的窗口关闭事件?
该窗口是父实例,而不是任何子实例。即,如果错误关闭了窗口,并且访问者再次启动浏览器并再次加载之前访问过的 URL。
Possible Duplicate:
javascript detect browser close tab/close browser
Does anyone know a reliable way to listen out for a window closing event in javascript/ jQuery?
The window is the parent and not any child instances. I.e. if a window is closed by mistake and the visitor launches their browser again and loads the url previously visited once more.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 window.unload 事件设置 cookie 或使用本地存储来使用 new.date() 保存时间,然后查看访问者是否在设定的时间内返回。
像这样的事情:
然后在加载时检查:
需要大量改进,并且本地存储应该有 cookie 作为后备,但只是为了显示它的要点。
You can use the window.unload event to set a cookie or use local storage to save the time using new.date(), then see if the visitor returned within a set amount of time.
Something like:
then on load check for :
Would need to be refined a lot, and local storage should have cookies as fallback, but just to show the jist of it.
尝试卸载方法。
您还可以尝试使用 JS onunload 和 onbeforeunload 事件。
Try the unload method.
You can also try playing with the JS onunload and onbeforeunload events.