Use localStorage through subdomains

I am replacing localStorage cookies in browsers that can support it (someone other than IE). The problem is site.com and www .site.com, which store their own localStorage objects. I think www is considered a subdomain (a stupid solution if you ask me). If the user was originally on site.com and decides to enter www .site.com on the next visit, all her personal data will not be available. How to get all my โ€œsubdomainsโ€ for sharing the same local storage as the main domain?

+48
javascript local-storage subdomain
Oct 26 2018-10-26
source share
6 answers

This is how I use it in domains ...

  • Use an iframe from the parent domain - say parent.com
  • Then on each child.com domain, just do postMessage on your parent.com iframe
  • All you have to do is set up the postMessage message interpretation protocol to talk to iframe parent.com.

Hope this helps :)

+45
Oct 11 2018-11-11T00:
source share

I propose making the .com website redirected to www.site.com for consistency and to avoid such problems.

Also consider using a cross-browser solution like PersistJS , which can use each browserโ€™s own repository.

+15
Oct 26 '10 at 18:00
source share

If you use the iframe and postMessage solution only for this particular problem, I think that it can be less work (both by code and by calculation) only with storing data in a file with a subdomain and if it is not already in localStorage when when downloading, take it from the cookie .

Pros:

  • No additional iframe or postMessage settings required.

Minuses:

  • Allows you to make data available in all subdomains (not just www), so if you do not trust all subdomains, they may not work for you.
  • Sends data to the server for each request. Not really, but depending on your scenario, perhaps even less work than the iframe / postMessage solution.
  • If you do, why not just use cookies directly? Depends on your context.
  • Cookie size 4K max, total for all cookies for the domain (thanks Blake for pointing this out in the comments).

I agree with other commentators, but it seems like this should be a specific option for localStorage, so jobs are not required.

+13
Jan 22 '15 at 18:05
source share

I use xdLocalStorage, it is a lightweight js library that implements LocalStorage interface and supports cross-domain storage using iframe post message message (angularJS support)

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

+2
Dec 10 '15 at
source share

This is how I solved it for my site. I redirected all pages without www to www.site.com. That way he will always use localstorage www.site.com

Add the following to your .htacess , (create it if you don't already have it) in the root directory

RewriteEngine On RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 
0
Mar 22 '17 at 8:31
source share

You can simply use the document.domain property. If you put this in your first JavaScript actions:

 document.domain="mysite.com"; 
-9
Dec 19 '12 at 22:26
source share



All Articles