Reputation: 12900
localStorage.clear()
seems to behave in a manner that completely wipes all local storage. Local Storage seems to fit my use case, but I'm concerned of the possibility the local storage isn't guaranteed to not be cleared outside of my control.
For example:
My web site has a line of code:
localStorage.setItem('some', 'data');
Some other website that is operating in the same browser on another tab has this line of code:
localStorage.clear();
If that scenario ever happens, when I go to access localStorage.getItem('some');
, it will return null
because someone else ran localStorage.clear()
.
localStorage.clear()
?localStorage.clear()
if I am heavily
using local storage and don't want to write a ton of code to remove
items from the local storage individually
localStorage.removeItem('item1'); localStorage.removeItem('item2');
// etc...
localStorage.clear()
somewhere else?Upvotes: 2
Views: 918
Reputation: 50291
It is unique per domain. For example the localStorage
for your domain
protocol://abc.com
will be different from protocol://xyz.com
Upvotes: 2
Reputation: 2826
There is no need to worry, every time you open a new window or tab a new session is created and localStorage refers to the local session of that tab/window which is unique for that domain.
Your fine running it, it will only clear your variables
If you need to clear everything use feel free to use localStorage.clear()
Someone else cannot clear your local storage
https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API
Upvotes: 3