- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to delete a localStorage item when the browser window/tab is closed?
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.
Advertisements