可以在另一个HTML文件中使用在一个HTML文件中创建和保存的localStorage数据吗? [英] Can localStorage data created and saved in one HTML file can be used in another HTML file?

查看:151
本文介绍了可以在另一个HTML文件中使用在一个HTML文件中创建和保存的localStorage数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用localStorage创建一个完全脱机的HTML5应用。

I am creating a completely offline HTML5 app using localStorage.

我已成功保存并检索和操作单个.HTML文件中的localStorage数据。无论如何,现在当我试图访问另一个.HTML文件中的一个.HTML文件存储的localSotrage数据时,我没有成功。

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.

无论如何,我创建了一个单独.html文件清除所有localStorage,并且那个工作正常。

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

是因为每个网页的localStorage是独立的,不能放在一起或其他一些错误?

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

推荐答案

localStorage由同源策略控制
假设您正在使用 file:// 因浏览器而异:

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

Webkit:

全部 file:// 文件来源相同
https://bugs.webkit.org/show_bug.cgi?id=20701

Mozilla:


在Gecko 1.8或更早版本中,任何两个文件:URI都被认为是
同源。换句话说,本地磁盘上的任何HTML文件都可以
读取本地磁盘上的任何其他文件。

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.

从Gecko 1.9开始,文件可以读取只有某些其他的
文件。具体来说,只有当原始文件的父
目录是
目标文件的祖先目录时,文件才能读取另一个文件。但是,无法以这种方式加载目录。

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:

不适用(参见: IE中未定义localStorage对象

这篇关于可以在另一个HTML文件中使用在一个HTML文件中创建和保存的localStorage数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆