Javascript localStorage和域 [英] Javascript localStorage and domains

查看:294
本文介绍了Javascript localStorage和域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为iPad/iPhone不支持来自第三方站点的cookie,所以我想将值存储在localStorage中.关于domainA的示例可能是:

Because iPad/iPhone doesn't support cookies from third party sites, I want to store values in localStorage. An example on domainA might be:

<script src="http://domainB/something.js"></script>

domainB上的此脚本可以访问window.localStorage,并且效果很好.但是,这些值存储在domainA中,因为这是文档的位置.

this script on domainB can access window.localStorage and it works great. However the values are stored in domainA because that's the document's location.

如果我将脚本放在源位于domainB的iframe内,则它可以工作,但我正尝试避免使用框架.所以我的问题是:有没有一种方法可以在远程域的localStorage中获取/设置值,以便以后我访问domainB时它们会在那里?

If I put my script inside an iframe whose source is on domainB, then it works, but I'm trying to avoid frames. So my question is: Is there a way to get/set values in localStorage on a remote domain such that they'll be there when I visit domainB at a later time?

推荐答案

没有iframe解决方法,这是不可能的.

This isn't possible without your iframe workaround.

从多个域访问同一个localStorage对象的能力将违反同一个起源策略和规范:

The ability to access the same localStorage object from multiple domains would violate the Same origin policy,and the spec:

用户代理必须引发SECURITY_ERR 任何成员都例外 最初的存储对象 由localStorage属性返回 被其脚本访问的脚本 有效的脚本来源不是 与...的原件相同 在其上的Window对象 已访问localStorage属性.

User agents must raise a SECURITY_ERR exception whenever any of the members of a Storage object originally returned by the localStorage attribute are accessed by scripts whose effective script origin is not the same as the origin of the Document of the Window object on which the localStorage attribute was accessed.

http://dev.w3.org/html5/webstorage/#security- localStorage

如果这不是用于iPhone,我建议使用Flash解决方案.但是使用localStorage之前,我认为您很不走运,直到他们实施跨域策略.

If this wasn't for iPhone, I would suggest a flash solution. But with localStorage, I think you are out of luck until they implement a cross-domain policy.

这篇关于Javascript localStorage和域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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