Can localStorage data created and saved in one HTML file can be used in another HTML file?

こ雲淡風輕ζ 提交于 2019-12-23 15:12:56

问题


I am creating a completely offline HTML5 app using localStorage.

I have successfully saved and retrieved and manipulated localStorage data in a single .HTML file. Anyhow, now when I am trying to access localSotrage data stored by one .HTML file on another .HTML file, I am getting no success.

Anyhow, I have created a separate .html file to clear all the localStorage, and that one is working fine.

Is it because localStorage of each web page is separate and cannot be put together or some other error?


回答1:


localStorage is controlled by same-origin policy Assuming you're using file:// it varies by browser:

Webkit:

All file:// documents have the same origin https://bugs.webkit.org/show_bug.cgi?id=20701

Mozilla:

In Gecko 1.8 or earlier, any two file: URIs are considered to be same-origin. In other words, any HTML file on your local disk can read any other file on your local disk.

Starting in Gecko 1.9, files are allowed to read only certain other files. Specifically, a file can read another file only if the parent directory of the originating file is an ancestor directory of the target file. Directories cannot be loaded this way, however.

https://developer.mozilla.org/en-US/docs/Same-origin_policy_for_file:_URIs

IE:

Doesnt apply (see: localStorage object is undefined in IE)



来源:https://stackoverflow.com/questions/20368160/can-localstorage-data-created-and-saved-in-one-html-file-can-be-used-in-another

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