Using session-storage across subdomains

天涯浪子 提交于 2019-12-29 08:46:16

问题


I have two domains domain1.site.com and domain2.site.com, i have set session-storage in domain1.site.com and then not able to get session-storage on other domain domain2.site.com from same tab. Is there any other way which can be used identify a tab when navigated across sub-domains?


回答1:


The question is a bit older, but maybe the following helps others.

There is a solution called "Cross Domain Local Storage", it works with a combination of localStorage and postMessage. With that you have a storage you can use with any domain. There are two projects that I know:

  • https://github.com/ofirdagan/cross-domain-local-storage
  • https://github.com/zendesk/cross-storage.

But that's not a session storage. To separate multiple tabs, you have to generate a unique id (maybe tab creation date in milliseconds) and save that into the session storage, but only the first time, when there is no value already in the session storage. That way you have one id per tab. That id you can use to put data into the cross domain local storage.




回答2:


As @PaulS mentioned in his comment, you most likely mean to use localStorage.

That said, you are still hindered by the Same Origin Policy.

If your example is correct, and both sites share the same top-level domain such as:

domain1 .example.com
domain2 .example.com

Then you can set document.domain = "example.com"; to allow the two to share information.

More detail on MDN: https://developer.mozilla.org/en-US/docs/Web/API/document.domain



来源:https://stackoverflow.com/questions/25064233/using-session-storage-across-subdomains

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