javascriptjquerylocal-storagewindow.onunload

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


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?


Solution

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