Using session-storage across subdomains
Asked Answered
S

2

10

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?

Shepherd answered 31/7, 2014 at 16:25 Comment(1)
First off, sessionStorage doesn't go across concurrent tabs anyway, you probably want localStorage for this. Secondly, no; these are of different origin so they don't share this. You may, however, be able to windowRef.postMessage data from one to the otherIntercalation
C
4

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:

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.

Conjunct answered 20/1, 2017 at 13:59 Comment(0)
C
-3

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

Cheese answered 31/7, 2014 at 16:50 Comment(5)
@Paul, I am looking for storage which is restricted only in the tab where it got created, so I believe that we should be using sessionStorage instead of localStorage to fulfill that purpose. And i have already tried to set the document.domain to a common top-level domain and checked the sessionStorage, it didnt work.Shepherd
What are the two domains that you are using, and what are you setting document.domain to? That combo really should work.Cheese
i have set the document.domain to top-level domain as you have mentioned, but i am checking the `sessionStorage'. Btw i tried this on Chrome.Shepherd
Tried this as well and had no luck.Rebarebah
it does not work at all :(. Moreover the link you gave does not document this behavior with storage sharing for the same document.domain.Sporty

© 2022 - 2024 — McMap. All rights reserved.