1

I have a task in which I need to transfer the value of a variable from a subdomain to a domain. I am using localstorage. I have a domain, and I have a subdomain, which means that each has its own script file, template file, etc.

In the subdomain script file, I write the following, i.e. I put the value of the variable into the localstorage cell:

/subdomain script file/

localStorage.setItem("city_id", city_id);

In the domain script file, I am trying to get this value like this:

/domain script file/

var result = localStorage.getItem("city_id");
console.log(result);

But the console displays undefined.

Has already worked with localstorage, and more than once. But one thing is not clear to me, if this is a browser storage, then there is no difference where you enter the value, and where you try to get it (I'm talking about different script files).

Maybe I missed something, or I don't fully understand how localstorage works. If localstorage does not work that way, please tell me other methods of transferring the value of a variable from a subdomain to a domain, or vice versa. Thank.

s.kuznetsov
  • 13,325
  • 3
  • 8
  • 21
  • could be the local storage respects cookie rules on sub domains – GetSet Aug 25 '20 at 04:16
  • As such, you could implement your own "local storage cache" server side. – GetSet Aug 25 '20 at 04:18
  • 2
    LocalStorage does not allow data to be shared from different domains. There are solutions available like [cross-domain-local-storage](https://github.com/ofirdagan/cross-domain-local-storage) or you can [use cookies that are shared](https://stackoverflow.com/a/23086139/2713655) between the two domains. – butttons Aug 25 '20 at 04:20

0 Answers0