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?
- localStorage values will not clear when you closing a tab or browser.Jeff Johny– Jeff Johny2015-11-03 11:55:55 +00:00Commented Nov 3, 2015 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– Rahul Gupta2015-11-03 12:12:29 +00:00Commented Nov 3, 2015 at 12:12
- "LocalStorage that is closed when browser is closed" is the exact definition of SessionStorage.Paul-Jan– Paul-Jan2015-11-03 12:30:42 +00:00Commented Nov 3, 2015 at 12:30
- 1i 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– Rahul Gupta2015-11-03 12:58:13 +00:00Commented Nov 3, 2015 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– Frenk2015-11-03 15:47:34 +00:00Commented Nov 3, 2015 at 15:47
Add a comment |
2 Answers
You should take a look at sessionStorage: https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API#sessionStorage