# Detecting the browser quit event (using sdk)

**URL:** <https://discourse.mozilla.org/t/detecting-the-browser-quit-event-using-sdk/7762>\
**Category:** Add-on Support\
**Created:** [March 26, 2016, 11:02am UTC](https://discourse.mozilla.org/t/detecting-the-browser-quit-event-using-sdk/7762 "2016-03-26T11:02:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![amitanilkulkarni](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/amitanilkulkarni/32/21146_2.png) [@amitanilkulkarni](https://discourse.mozilla.org/u/amitanilkulkarni)\
**Post date:** [March 26, 2016, 11:02am UTC](https://discourse.mozilla.org/t/detecting-the-browser-quit-event-using-sdk/7762/1 "2016-03-26T11:02:35Z")

</div>

For one of my add-ons, I need to be able to detect browser quit event, and send a logout request to a page before it closes.

I am using add-on sdk. The [page about working with events](https://developer.mozilla.org/en-US/Add-ons/SDK/Guides/Working_with_Events) doesn’t say anything about a browser close event.

Is it even possible to do this using sdk?

Thanks!

---

<div class="post-metadata">

**Author:** ![jorgev](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@jorgev](https://discourse.mozilla.org/u/jorgev)\
**Post date:** [March 29, 2016, 7:54pm UTC](https://discourse.mozilla.org/t/detecting-the-browser-quit-event-using-sdk/7762/2 "2016-03-29T19:54:50Z")

</div>

You can use the low-level [system/events API](https://developer.mozilla.org/en-US/Add-ons/SDK/Low-Level_APIs/system_events) and any of [these observer notifications](https://developer.mozilla.org/en-US/docs/Observer_Notifications).

---

<div class="post-metadata">

**Author:** ![amitanilkulkarni](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/amitanilkulkarni/32/21146_2.png) [@amitanilkulkarni](https://discourse.mozilla.org/u/amitanilkulkarni)\
**Post date:** [April 9, 2016, 2:54pm UTC](https://discourse.mozilla.org/t/detecting-the-browser-quit-event-using-sdk/7762/3 "2016-04-09T14:54:53Z")

</div>

Thanks!

I tried it out, and it doesn’t always seem to work. I am testing it with this script:

```
var ev = require("sdk/system/events");

ev.on("quit-application-requested", function(eSubject, eTopic, eData) {
    console.log(eSubject.type);
});

```

If I just close the browser without creating a new tab or accessing some page, I can see the output as:

`console.log: logout-test: quit-application-requested`

However, it fails pretty much always if I access some page, or open a new tab or window before hitting the close button. Even ctrl+q doesn’t seem to do anything differently.

If I open a new hidden frame in the background, will it close when I close the last browser window? Can I make use of that in any way? Perhaps by tracking when that hidden frame is about to be closed?

Thanks!

---

<div class="post-metadata">

**Author:** ![jorgev](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@jorgev](https://discourse.mozilla.org/u/jorgev)\
**Post date:** [April 12, 2016, 10:15pm UTC](https://discourse.mozilla.org/t/detecting-the-browser-quit-event-using-sdk/7762/4 "2016-04-12T22:15:40Z")

</div>

You might want to try with `quit-application-granted`. That’s called when it’s clearer that the application will close.

> [@amitanilkulkarni](#):
>
> If I open a new hidden frame in the background, will it close when I close the last browser window? Can I make use of that in any way? Perhaps by tracking when that hidden frame is about to be closed?

That could work, but I don’t know how that would work with the SDK, since it doesn’t have the same concept of a background page as WebExtensions or Chrome Extensions do.

---

<div class="post-metadata">

**Author:** ![amitanilkulkarni](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/amitanilkulkarni/32/21146_2.png) [@amitanilkulkarni](https://discourse.mozilla.org/u/amitanilkulkarni)\
**Post date:** [April 13, 2016, 7:34am UTC](https://discourse.mozilla.org/t/detecting-the-browser-quit-event-using-sdk/7762/5 "2016-04-13T07:34:42Z")

</div>

Thanks!  
Tried your suggestion, but same problem again. Could you please check if this is a bug?

- Use the script above. Run using `jpm -b /usr/bin/firefox run`.
- Open any page in the browser and close it however you want.

Console doesn’t show the `quit-application-granted` event. If you close the browser **without** opening any new page, it works.

Is it just my system?
