dsp_099
dsp_099

Reputation: 6121

How to store a browser-wide localStorage value?

Say I open Chrome and hop into the javascript console. If I run

localStorage.setItem('test', 'THIS IS A VALUE');

and then close the browser, re-open it, then run

localStorage.getItem('test');

the result will be, predictably 'THIS IS A VALUE'. However, if I were to navigate to a different page that I was on when I opened the browser, ie NOT on about:blank; or whatever homepage it might be, running .getItem will result in a null.

It seems like localStorage is specific to a certain page. Is there a way to store a value on one page that will work on ANY page?

Say that I want to generate an ID on one page, and then check for the same one on a totally unrelated site, is this possible? Sounds like a bit of a security risk now that I've typed it out.

Upvotes: 3

Views: 1138

Answers (1)

user149341
user149341

Reputation:

There's a separate LocalStorage store per-domain, much like cookies. Consider the alternative: if LocalStorage were global, different web sites could read and write each other's stored values.

Storing persistent data that "tracks" the user across multiple sites is, somewhat intentionally, difficult. You will need to explicitly take an action that crosses domains (e.g, loading content from, or posting data to, another site) to achieve this.

Upvotes: 3

Related Questions