I've already tried window.unload, window.beforeunload, etc. I'm looking for a way to notify my background page once the popup is closed.
-
Have you tried [`chrome.tabs.onRemoved`](http://developer.chrome.com/extensions/tabs.html#event-onRemoved) or maybe [`chrome.windows.onRemoved`](http://developer.chrome.com/extensions/windows.html#event-onRemoved) if it counts as a window? – BeardFist Apr 03 '13 at 22:12
-
It doesn't count as a window. – ebi Apr 04 '13 at 02:24
-
1See also: [Does onbeforeunload event trigger for popup.html in a google chrome extension?](http://stackoverflow.com/q/2315863/405550) – Zaz Oct 17 '16 at 14:39
8 Answers
You can try this. Connect to your background page with chrome.runtime.connect (or chrome.extension.connect
before Chrome 26) and port.onDisconnect
will be fired in your background page when the popup is closed.

- 4,042
- 1
- 24
- 28
-
`port.onDisconnect` fires when the extension is removed/deleted not when the user stops using it, and the popup closes. – Mudlabs Aug 15 '17 at 02:54
-
-
-
-
1FYI This use case is now broken in manifest v3 as ports will be closed automatically after some time and you'll get errors saying you're trying to connected a disconnected port object. – Eric Uldall Oct 21 '22 at 00:11
It's as simple as that:
// popup.js
chrome.runtime.connect({ name: "popup" });
// background.js
chrome.runtime.onConnect.addListener(function(port) {
if (port.name === "popup") {
port.onDisconnect.addListener(function() {
console.log("popup has been closed")
});
}
});
Note that there are some edge cases where this method doesn't work. E.g. when having the popup open and then switching tabs by using a keyboard shortcut such as ctrl + t for example.

- 1,763
- 1
- 14
- 24
Probably a hacky way, but in the popup page you can listen to
window.onblur = function(){}
and send a message to active tab.

- 1,942
- 14
- 15
-
Unfortunately this triggers when switching app even with the popup still remaining open, but does not trigger when actually closing the popup by clicking the extension icon or current tab – Nick Cardoso Jul 01 '19 at 07:41
-
There is currently no way to figure out when the browser action popup was
closed as window.unonload
is triggered immediately when the popup finished
loading, not when it's closed. There is a bug crbug.com/31262 for this.
Three work arounds available are well described here. They include the port.onDisconnect
trick and periodically polling chrome.extension.getViews()
from either the popup or a background page.

- 25,267
- 15
- 124
- 150
I'm surprised noone mentioned this (I've tested it on Firefox Nightly and it works there):
window.addEventListener("pagehide", function(event) {
console.log(event);
});

- 414
- 5
- 13
-
Doesn't work for me in Chrome 96.0.4664.110. I've also tried `visibilitychange` event but it works inconsistently. Moving back to port.onDisconnect hack. – Dmitry Davydov Jan 04 '22 at 03:37
Try to use the visibilitychange event on the document object. It works fine for me:
document.addEventListener('visibilitychange', MyFunction, false);

- 19
- 1
This is the solution I used for manifest V3:
Set up a connection in your popup's JavaScript file
chrome.runtime.connect();
In background.js, use onConnect
to listen for the event
chrome.runtime.onConnect.addListener(port => {
port.onDisconnect.addListener(()=>{
//Your event will be fired in here
})
})

- 21,353
- 33
- 103
- 168

- 19
- 2
Finally found the solution. Put below code in background.js
/eventPage.js
:
chrome.windows.onFocusChanged.addListener(function(window) {
//handle close event
});

- 42,508
- 29
- 229
- 225
-
4This will fire when any window or extension gets or loses focus. Doesn't answer the question. – Joe Martella Mar 20 '18 at 21:27
-
Helped with my related issue, so it's at least indirectly relevant IMHO... – robertdavid Jul 08 '21 at 21:32