Javascript localStorage and domains

落花浮王杯 提交于 2019-12-21 09:04:16

问题


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>

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.

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?


回答1:


This isn't possible without your iframe workaround.

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

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

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.



来源:https://stackoverflow.com/questions/4328252/javascript-localstorage-and-domains

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!