html - 在一个 HTML 文件中创建和保存的 localStorage 数据能否在另一个 HTML 文件中使用?

标签 html local-storage

我正在使用 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

即:

不适用(参见:localStorage object is undefined in IE)

关于html - 在一个 HTML 文件中创建和保存的 localStorage 数据能否在另一个 HTML 文件中使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20368160/

相关文章:

javascript - 处理来自客户端存储的数据以进行页面渲染

css - 如何在 Web 应用程序中渲染多个尺寸的同一图像?

android - (如何)我可以在 Android WebView 中增加 LocalStorage 的配额限制

html - 为什么 SVG 元素内部会出现这个空白区域?

javascript - 如何删除数据表中本地存储中表中的特定行

javascript - 包含本地 session 变量的 Scriptlet

html - 调整侧边栏旁边 <div> 中的图片大小

html - 如何将div一个放在另一个下面?

html - 网站在大多数浏览器上无法正确缩放,大多数元素都放错了位置

html - 显示:table element的绝对定位