html5本地存储仅返回主页上的值,而不返回其他页面的值
这是我第一次使用 localStorage,我正在尝试存储一个我正在 stringify(ing) 的 json 对象。
在我的主页上,我使用以下代码
localStorage.setItem("users",JSON.stringify(userList)); alert(localStorage.getItem("users")); location.href="test2.html";
此警报吐出用户列表没有问题。
然后在 test2.html 上,我让
var userList=JSON.parse(localStorage.getItem("users")); alert(userList.toSource());
Firebug 吐出“userlist is null”错误。
我尝试使用字符串“test”代替 strigified json 对象,但没有成功。我还尝试删除 JSON.parse 并返回字符串。这也行不通。
我做错了什么?
This is my first time working with localStorage, and I'm trying to store a json object which I am stringify(ing).
On my home page, I use the following code
localStorage.setItem("users",JSON.stringify(userList)); alert(localStorage.getItem("users")); location.href="test2.html";
This alert spits out the userlist no problem.
Then on the test2.html, I have
var userList=JSON.parse(localStorage.getItem("users")); alert(userList.toSource());
Firebug spits out a 'userlist is null' error.
I've tried using the string 'test' in place of the strigified json object, but no luck. I've also tried removing the JSON.parse and just get the string returned. This also didn't work.
What am I doing wrong??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现代浏览器已经决定,本地文件(换句话说,“file://”URL)不都被视为公共“域”的成员,至少对于某些目的而言是如此。 Chrome 似乎是最严格的,但也许在 HTML5 特性上 Firefox 也同样严格。
这是有道理的:如果有人发明了某种类似 TiddlyWiki 的工具,使用 HTML5 存储,您不会希望它的内容可供您出于完全不相关的原因下载的其他随机 HTML 文件使用。
Modern browsers have decided that local files ("file://" URLs, in other words) are not all to be considered members of a common "domain", at least for some purposes. Chrome seems to be the strictest, but maybe in the HTML5 features Firefox is also as strict.
It kind-of makes sense: if somebody invents some kind of TiddlyWiki-like tool that uses HTML5 storage, you wouldn't want its stuff to be available to some random other HTML file you download for totally unrelated reasons.