I want to clear the local storage when a tab is closed. If I use $(window).unload
, it'll clear the storage also when the page is refreshed. Is there a way to do it only if a tab is closed?
Asked
Active
Viewed 84 times
1

Andre Nevares
- 711
- 6
- 21

brainoverflow
- 691
- 2
- 9
- 22
-
1As far as i know there is no way to know if user refreshing the page or closing the app – halilcakar Jun 09 '20 at 20:58
-
2Can you instead use sessionStorage and let the browser handle clearing the data when the tab is closed? [MDN: Window.sessionStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage) – Ed Lucas Jun 09 '20 at 21:37
-
Thank you @EdLucas ! Please post your comment as an answer so I can accept it! – brainoverflow Jun 09 '20 at 21:59
1 Answers
1
If you're only looking to use this event to clear your localStorage, you might consider using sessionStorage instead. The difference is that the browser will handle clearing the data when the tab or window is closed (the data will be still be retained when refreshing).

Ed Lucas
- 5,955
- 4
- 30
- 42