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.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. I am not looking for this to work in any other browser. Looking for a solution only in chrome
window.addEventListener("beforeunload", function (e) {
var confirmationMessage = "See you later" ;
(e || window.event).returnValue = confirmationMessage;
return confirmationMessage;
}
);
A tab or window closing in a browser can be detected by using the beforeunload event. This can be used to alert the user in case some data is unsaved on the page, or the user has mistakenly navigated away from the current page by closing the tab or the browser.
Close tab with your keyboard's function keys The active tab closes, and your browser will make the first open tab the active one. If you have no other open tabs, it closes your browser.
When we refresh the page (F5, or icon in browser), it will first trigger ONUNLOAD event. When we close the browser (X on right top icon),It will trigger ONUNLOAD event.
You can use the onunload event handle to detect this.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With