1

I used window.onunload and tried window.onbeforeunload but both are clearing the data on tab close as well. I want the data to remain when tab is closed and it should clear it out when the entire browser is closed. Any solution on this?

Rahul Gupta
  • 972
  • 11
  • 29
  • localStorage values will not clear when you closing a tab or browser. – Jeff Johny Nov 03 '15 at 11:55
  • yes... but i want to clear it, which i did but wanted to clear when windows is closed not tab.. – Rahul Gupta Nov 03 '15 at 12:12
  • "LocalStorage that is closed when browser is closed" is the exact definition of SessionStorage. – Paul-Jan Nov 03 '15 at 12:30
  • 1
    i used session storage as well but the scope of storage is remain on the same page... if you open any other page on the same browser, you won't get the storage value. – Rahul Gupta Nov 03 '15 at 12:58
  • You could try setting a unique identifier in a session cookie. Use that identifier as key in the localStorage. When closing the browser, the cookie is removed and so is the reference to the localStorage item :) – Frenk Nov 03 '15 at 15:47

2 Answers2

1

That's not possible. Unluckily there is no event fired when closing the entire browser (at least not in Chrome, Firefox and Internet Explorer).

You will have to find another solution to achieve what you need :(

Matto
  • 108
  • 1
  • 2
  • 13
1

You should take a look at sessionStorage: https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API#sessionStorage

Frenk
  • 91
  • 1
  • 6