I’ve worked with localStorage across multiple windows before without any problems, but for some reason in this new application it is not working at all.
I have two pages, PageA and PageB. PageA sets some data in localStorage like so:
var someData = {
item1: 'foo',
item2: 'bar'
};
window.localStorage.setItem('someKey', JSON.stringify(someData));
If I immediately try to read the data via JSON.parse(window.localStorage.getItem('someKey'));, it works fine.
PageA then calls a window.open(PageBURL) to open PageB. In PageB, when I attempt to read the value from localStorage, all I get is null:
JSON.parse(window.localStorage.getItem('someKey')); // gives me null
Both pages have identical document.domain values. There must be something stupid I am missing, does anyone have any ideas of things to double check?
Turns out
localStorageruns under the full host domain, not the javascriptdocument.domain.PageA runs under
"foo.common.net"with adocument.domainof"common.net".PageB runs under
"bar.common.net"with adocument.domainof"common.net".If I switch PageA to run under
"bar.common.net", I am able to read the data without a problem.