- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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.
- Related Articles
- Execute a script when the browser window is closed in HTML?
- Execute a script when the browser window is being resized in HTML?
- Which is the event when the browser window is resized in JavaScript?
- How to store data in the browser with the HTML5 localStorage API?
- JavaScript: How to return True if the focus is on the browser tab page?
- How to Resize Browser Window in WebDriver?
- How to create a browser window example with CSS?
- How to delete Tkinter widgets from a window?
- Open New Browser Tab in Selenuim
- How to set the size of the browser window in Selenium?
- How to close active/current tab without closing the browser in Selenium-Python?
- How to auto delete YouTube history on browser?
- HTML DOM Window closed Property
- How to maximize the browser window in Selenium WebDriver using C#?
- How to open a link in new tab of chrome browser using Selenium WebDriver?

Advertisements