简体   繁体   English

浏览器窗口关闭并关闭标签页

[英]Browser window close and tab close

I want to create a JS function that detect only browser close and tab close functionality. 我想创建一个仅检测浏览器关闭和选项卡关闭功能的JS函数。 I used this code from http://ykyuen.wordpress.com/2011/02/22/jquery-javascript-capture-the-browser-or-tab-closed-event/ 我使用了来自http://ykyuen.wordpress.com/2011/02/22/jquery-javascript-capture-the-browser-or-tab-closed-event/的代码

I works for me but when i place a cursor on address and hit enter it goes to kill page method. 我为我工作,但是当我将光标放在地址上并按Enter时,它将终止页面方法。 I need only to show alert when tab or browser is closed. 当标签或浏览器关闭时,我只需要显示警报。

I am afraid this is not possible. 恐怕这是不可能的。

You cannot really make a distinction. 您无法真正区分。 When the user navigates away from your page in any way, you get the same events ( beforeunload , unload ). 当用户以任何方式离开页面时,您都会收到相同的事件( beforeunloadunload )。 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. 无论用户单击页面上的任何链接还是提交您的表单之一,您都可以使用一些技巧(例如在发布的链接中),但是您无法真正区分刷新按钮,返回按钮,用户输入新地址,用户单击书签,关闭选项卡,关闭浏览器等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM