Fail to read localStorage

2019-08-21 09:20发布

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条回答
The star\"
2楼-- · 2019-08-21 09:47

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?

查看更多
登录 后发表回答