Delete localStorage when closing javascript browser

I found the answer using window.onbeforeunload to handle closing the browser. But this is also an action when updating the browser. I just want to delete my local storage when the browser is not updating.

+4
source share
3 answers

You can use sessionStorage instead of localStorage .

sessionStorage clears automatically when the browser is closed.

It works almost the same as localStorage:

 sessionStorage.setItem("Test", "Test"); var item = sessionStorage.getItem("Test"); 
+16
source

I think you are looking for sessionStorage not localStorage

+3
source

If you want to save some data that will be used only for this particular session, say that the user login, you need to use sessionStorage, but if your requirement is that you want to allow the user to open several tabs, then SessionStorage will not be because sessionStorage is not used in different tabs, for this you will need to use localStorage.

Be sure to keep the number of open and closed tabs and clear the local storage when the last tab is closed.

+3
source

Source: https://habr.com/ru/post/1500700/


All Articles