How to delete a localStorage item when the browser window/tab is closed?

JavascriptFront End TechnologyObject Oriented Programming

To clear a localStorage data on browser close, you can use the window.onunload event to check for tab close.

Let's say you have a local storage object called MyStorage as a global for the sake of this example. Then you can write an event handler −

Example

window.onunload = () => {
   // Clear the local storage
   window.MyStorage.clear()
}

This will clear the local storage on the tab/window close.

raja
Published on 27-Nov-2019 15:14:38
Advertisements