简体   繁体   English

Chrome - 检测浏览器关闭或标签页关闭

[英]Chrome - Detect browser close or tab close

I am using following two listeners on my page when a user closes a tab or window in chrome, but they don't seem to work当用户关闭 chrome 中的选项卡或窗口时,我在我的页面上使用以下两个侦听器,但它们似乎不起作用

chrome.tabs.onRemoved.addListener(function (integer tabId, object removeInfo) {alert("Haooooo")});

chrome.windows.onRemoved.addListener(function (integer windowId) {alert("Haooooo")});

But, the following function detects the window close or tab close, but gets triggered on refresh too.但是,以下函数检测窗口关闭或选项卡关闭,但也会在刷新时触发。 Has any one have a way to detect the browser/tab close only for Chrome.有没有一种方法可以检测仅针对 Chrome 关闭的浏览器/标签页。 I am not looking for this to work in any other browser.我不希望它在任何其他浏览器中工作。 Looking for a solution only in chrome仅在 chrome 中寻找解决方案

window.addEventListener("beforeunload", function (e) {          
          var confirmationMessage = "See you later" ;
          (e || window.event).returnValue = confirmationMessage;
          return confirmationMessage;
}
);

Your syntax is invalid.您的语法无效。 It should be它应该是

chrome.tabs.onRemoved.addListener(function(tabid, removed) {
 alert("tab closed")
})

chrome.windows.onRemoved.addListener(function(windowid) {
 alert("window closed")
})

However, these apis will not work on normal webpages, only in extensions.但是,这些 api 不适用于普通网页,只能在扩展中使用。

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

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