Way to Solution That's because localstorage doesn't support sharing the storage across subdomains or even domain. Thus, if you have something stored at a.example.com it won't be accessible from example.com or b.example.com.
Solution: Use an iframe to save the data in localStorage, and then the other domains ask the iframe for what we already saved.
Why Local Storage is Insecure and You Shouldn't Use it to Store Sensitive Data. Here's the deal: most of the bad things about local storage aren't all that important. You can still get away with using it but you'll just have a slightly slower app and minor developer annoyance.
It is possible to do "cross-domain localStorage", but it would typically use an iframe with window. postMessage() to share data between domains.
This is how I use it across domains...
I hope it helps :)
If you're using the iframe and postMessage solution just for this particular problem, I think it might be less work (both code-wise and computation-wise) to just store the data in a subdomain-less cookie and, if it's not already in localStorage on load, grab it from the cookie.
I agree with other commenters though, this seems like it should be a specifiable option for localStorage so work-arounds aren't required.
I suggest making site.com redirect to www.site.com for both consistency and for avoiding issues like this.
Also, consider using a cross-browser solution like PersistJS that can use each browser native storage.
Set to cookie in the main domain -
document.cookie = "key=value;domain=.mydomain.com"
and then take the data from any main domain or sub domain and set it on the localStorage
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With