浏览器窗口关闭和标签页关闭 [英] Browser window close and tab close

查看:157
本文介绍了浏览器窗口关闭和标签页关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个仅检测浏览器关闭和选项卡关闭功能的JS函数. 我从 http://ykyuen.wordpress.com/2011/02/22/jquery-javascript-capture-the-browser-or-tab-closed-event/

I want to create a JS function that detect only browser close and tab close functionality. I used this code from http://ykyuen.wordpress.com/2011/02/22/jquery-javascript-capture-the-browser-or-tab-closed-event/

我为我工作,但是当我将光标放在地址上并按Enter时,它将终止页面方法.我只需要在关闭选项卡或浏览器时显示警报即可.

I works for me but when i place a cursor on address and hit enter it goes to kill page method. I need only to show alert when tab or browser is closed.

推荐答案

恐怕这是不可能的.

您无法真正区分.当用户以任何方式离开页面时,您都会收到相同的事件(beforeunloadunload).无论用户单击页面上的任何链接还是提交您的表单,您都可以使用一些技巧(例如在发布的链接中),但是您无法真正区分刷新按钮,返回按钮,用户输入新地址,用户单击书签,关闭选项卡,关闭浏览器等.

You cannot really make a distinction. When the user navigates away from your page in any way, you get the same events (beforeunload, unload). You can do some tricks (like in the link posted) whether the user clicked any of the links on your page or submitted one of your forms, but you cannot really differentiate between the refresh button, back button, the user entering a new address, the user clicking a bookmark, close tab, close browser, etc.

这篇关于浏览器窗口关闭和标签页关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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