HTML 5 Storage's same origin policy

2019-05-04 07:21发布

I've tried with Firefox 4.0 to use the localStorage object to save a few values used to fill the form at http://host1.example.com/index.html and to fetch those values to automatically fill the same form at http://host2.example.com/index.html but it doesn't work.

Does the same origin policy applies to the url instead of the domain?

4条回答
【Aperson】
2楼-- · 2019-05-04 07:48

you're out of luck. localStorage cannot be shared between different domains.

查看更多
Viruses.
3楼-- · 2019-05-04 08:03

So for example I could set localStorage data at dev.blah.com, and retrieve it from prod.blah.com, as long as I set the document.domain = "blah.com"; ?

查看更多
孤傲高冷的网名
4楼-- · 2019-05-04 08:05

You can't use localStorage or sessionStorage cross the domains or sub-domains just with the original API. You can use some libraries to help sending message between different domains.

查看更多
贪生不怕死
5楼-- · 2019-05-04 08:07

No, it applies to hosts. You could try setting document.domain before you create or retrieve your localStorage objects:

document.domain = "example.com";

--edit

OK, though you can set document.domain that has no impact on localStorage. It is possible to hack together a solution using iframe and cross document messaging: Cross-domain localStorage

查看更多
登录 后发表回答