# Save data to storage on popup close

**URL:** <https://discourse.mozilla.org/t/save-data-to-storage-on-popup-close/14893>\
**Category:** Development\
**Created:** [April 7, 2017, 10:25am UTC](https://discourse.mozilla.org/t/save-data-to-storage-on-popup-close/14893 "2017-04-07T10:25:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![miby](https://avatars.discourse-cdn.com/v4/letter/m/46a35a/32.png) [@miby](https://discourse.mozilla.org/u/miby)\
**Post date:** [April 7, 2017, 10:25am UTC](https://discourse.mozilla.org/t/save-data-to-storage-on-popup-close/14893/1 "2017-04-07T10:25:14Z")

</div>

I tried to save my changed data on popup close by calling chrome.storage.local.set on window.onunload, but it seems not to save the data. Is this intended not to work? If yes, is there any workaround?

---

<div class="post-metadata">

**Author:** ![NilkasG](https://avatars.discourse-cdn.com/v4/letter/n/0ea827/32.png) [@NilkasG](https://discourse.mozilla.org/u/NilkasG)\
**Post date:** [April 7, 2017, 10:32am UTC](https://discourse.mozilla.org/t/save-data-to-storage-on-popup-close/14893/2 "2017-04-07T10:32:42Z")

</div>

You could see if you are still able to send messages to the background page. If that doesn’t work either, you could try the `beforeunload` event instead, or, if your “popup” is the browserAction panel, even the `window.onblur` event, after all, the blur closes the panel in Firefox.

If none of those work either, you can try to call a function directly in the background page to do the saving. Just be aware hat that won’t work with private windows.

---

<div class="post-metadata">

**Author:** ![miby](https://avatars.discourse-cdn.com/v4/letter/m/46a35a/32.png) [@miby](https://discourse.mozilla.org/u/miby)\
**Post date:** [April 7, 2017, 10:58am UTC](https://discourse.mozilla.org/t/save-data-to-storage-on-popup-close/14893/3 "2017-04-07T10:58:58Z")

</div>

Yes I’m speaking about the browser action panal.  
So I tried the onbeforeunload event, but in contrast to the unload event it never gets called.  
However the onblur event or calling it directly in the background script seems to work nicely after some quick tests.

Thank you!
