Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does onbeforeunload event trigger for popup.html in a google chrome extension?

I'm writing a google chrome extension with a popup and a background page. The popup subscribes to certain events that the background generates, and I would like to unsubscribe from those events when the popup goes away. However, I don't see either of onbeforeunload or onunload events being generated from the popup. Are these events fired? If not, any ideas on how to capture popup close?

like image 966
praveen Avatar asked Feb 23 '10 03:02

praveen


2 Answers

"beforeunload" is not fired for browser action popups. Believe me, I tried. "unload" should be fired correctly, however, so listen for that.

Actually, I have a similar system in place, that allows you to attach events using addEventListener, and they are automagically cleaned up when the unload event fires.

Here's a little tip: obviously you can't use console.log, because by the time the unload event fires, it's already too late. But, you can do this:

var background = chrome.extension.getBackgroundPage();

addEventListener("unload", function (event) {
    background.console.log(event.type);
}, true);

By using the above code, you can open up the background page's console, which should show that the unload event is fired correctly.

like image 164
Pauan Avatar answered Sep 29 '22 07:09

Pauan


I've had a kind of similar issue. In my case, I've set the focus on an element which was in the popup page. So when the popup is closed, the element lose the focus and I can catch the blur event.

like image 39
Anthony Chatellier Avatar answered Sep 29 '22 06:09

Anthony Chatellier