Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove localStorage on closing browser javascript

I found most answer using window.onbeforeunload to handle browser closing. But it also does action when refreshing browser. I just want to remove my localStorage when browser closed not refreshing.

like image 257
Nothing Avatar asked Sep 05 '13 10:09

Nothing


People also ask

Does localStorage persist after closing browser?

The main features of localStorage are: Shared between all tabs and windows from the same origin. The data does not expire. It remains after the browser restart and even OS reboot.

How do I clear local storage in JavaScript?

To delete local storage sessions, use the removeItem() method. When passed a key name, the removeItem() method removes that key from the storage if it exists.

How can I clear the localStorage when user close react application window?

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

Does sessionStorage clear on tab close?

Closing a tab/window ends the session and clears objects in sessionStorage .


1 Answers

You can use the sessionStorage object instead of localStorage.

sessionStorage is cleared automatically when the browser is closed.

It works in pretty much the same way as localStorage:

sessionStorage.setItem("Test", "Test");

var item = sessionStorage.getItem("Test");
like image 68
Richard Dalton Avatar answered Sep 21 '22 12:09

Richard Dalton