I want to clear or remove my local storage when close the tab or browser not in refresh (f5) I have used localstorage.clear();. but this is clear cookies on f5. I only want when tab or browser close.
Asked
Active
Viewed 1,541 times
0
-
6Use sessionStorage instead, it will be cleared automatically. – dfsq Jan 11 '18 at 08:50
-
In session storage, cookies will clear in f5 also. but I only need closing the tab. – Aditi Jan 11 '18 at 09:25
-
Web storage and cookies are different. How can it be "clear also" ? – Duke Jan 11 '18 at 09:36
-
I want to clear local storage.I am using multi color theme. I want to clear selected theme, when browser close means default theme reflect when browser open. – Aditi Jan 11 '18 at 09:39
-
Possible duplicate of [How to delete a localStorage item when the browser window/tab is closed?](https://stackoverflow.com/questions/9943220/how-to-delete-a-localstorage-item-when-the-browser-window-tab-is-closed) – NickCoder Jun 26 '19 at 05:40
1 Answers
0
I am not sure about the syntax of jquery or JS, but you can try this:
//when browser closed - psedocode
$(window).unload(function(){
localStorage.removeItem(key);
// more keys here
});
Here, "key" is the keys of all the values which you are storing in local storage. Other than that you can use "delete localStorage.key" as well but I never used it so I can not explain in deep.

Gaurang Dave
- 3,956
- 2
- 15
- 34