localStorage is not working on google chrome

42,396

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');

Read more on MDN..

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.

Share:
42,396
Tushar
Author by

Tushar

Updated on July 09, 2022

Comments

  • Tushar
    Tushar almost 2 years

    I am using browsers localStorage to store a value, but while using google chrome, when we refresh the page using window.location.reload(), localStorage.value is flushed. e.g

    localStorage.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
    iPzard over 5 years
    They 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.