$(window).unload gets triggered by many events, how can I trigger an event only when a tab is closed?

111 Views Asked by At

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?

1

There are 1 best solutions below

0
Ed Lucas On BEST ANSWER

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).