我正在使用 localStorage 创建一个完全离线的 HTML5 应用。
我已在单个 .HTML 文件中成功保存、检索和操作 localStorage 数据。无论如何,现在当我试图访问一个 .HTML 文件存储在另一个 .HTML 文件上的 localSotrage 数据时,我没有成功。
无论如何,我已经创建了一个单独的 .html 文件来清除所有 localStorage,并且该文件工作正常。
是不是每个网页的localStorage是独立的,不能放在一起还是其他什么错误?
最佳答案
localStorage 由同源策略控制
假设您使用的是 file://
它因浏览器而异:
网络工具包:
所有file://
文件同源
https://bugs.webkit.org/show_bug.cgi?id=20701
摩斯拉:
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
即:
关于html - 在一个 HTML 文件中创建和保存的 localStorage 数据能否在另一个 HTML 文件中使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20368160/