Skip to content

Delete a localStorage item when the browser window/tab is closed

We can use the window.beforeunload event to find out if a tab is closed and then we can clear the local storage. beforeunload event gets triggered when a window or browser tab is about to close.

Advertisements
window.onbeforeunload = () => {
  localStorage.removeItem('isLoggedin');
}
See also  Diameter of Binary Tree - Leetcode Challenge - Python Solution

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.