Fail to read localStorage

牧云@^-^@ 提交于 2019-12-25 16:07:10

问题


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?



来源:https://stackoverflow.com/questions/46249215/fail-to-read-localstorage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!