Fail to read localStorage

2019-08-21 09:31发布

问题:

I have a plunker to set a value in localStorage:

<!DOCTYPE html>
<html>
  <script>
    localStorage.setItem('test', "hadddddha");
  </script>
</html>

And I have a code http://www.addbba.com/testLocalStorage.html to get it:

<!DOCTYPE html>
<html>
  <script>
    var test = localStorage.getItem('test');
    console.log(test)
  </script>
</html>

Oddly, the console shows the value is null. Does anyone know what happened?

回答1:

According to https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage localStorage is specific to a document's origin. Can you confirm that you are reading a value from localStorage using the same origin as where you wrote the value?