localStorage is not working on google chrome
Solution 1
LocalStorage supports only string values, not boolean or others.
Method to store and retrieve values:
Put the value into storage
localStorage.setItem('value1', 'true');
Retrieve the value from storage
var val1 = localStorage.getItem('value1');
Solution 2
You need to use the correct API for Storage
:
window.localStorage.setItem('value1', 'true');
What you're doing is setting a property on a variable which won't persist between page loads. Firefox is likely being too smart and recognizing that you want to actually save the value in the browser's local storage store.
Tushar
Updated on July 09, 2022Comments
-
Tushar almost 2 years
I am using browsers
localStorage
to store a value, but while using google chrome, when we refresh the page usingwindow.location.reload()
,localStorage.value
is flushed. e.glocalStorage.value1=true
after reloading, i am not getting this value1 object in localStorage.
Same code works on mozila firefox, but not in chrome. While using firefox, localstorage value is persistent.
-
iPzard over 5 yearsThey work if you set them to true (not a string) in Chrome. Is this for Firefox or thing or something? Want to make sure I'm using these correctly for all browsers.