Scope of sessionStorage and localStorage
Asked Answered
A

2

117

I read some documentation on sessionStorage and localStorage, but I don't understand what the scope is: the domain, a specific page?

For example, if I have the following pages:

http://example.com/products.aspx?productID=1

http://example.com/products.aspx?productID=2

http://example.com/services.aspx?serviceID=3

And if on each of the above pages I run (with idvalue being the value in the querystring):

localStorage.setItem('ID',idvalue);

Am I going to end up with 3 different values stored, or are the values going to overwrite each other?

Aerobe answered 16/3, 2012 at 18:8 Comment(0)
R
105

The values are going to overwrite each other. Each key-name pair is unique for a protocol and domain, regardless of the paths.

The affected domain can be changed via the document.domain property.

  • sub.example.com -> example.com is possible (subdomain)
  • sub.example.com -> other.example.com is not possible

References:

  • MDN: Web Storage API: "sessionStorage maintains a separate storage area for each given origin [...]"
  • MDN: Origin: "Two objects have the same origin only when the scheme, hostname, and port all match."
Revolution answered 16/3, 2012 at 18:11 Comment(6)
Thanks! Would you have a reference to recommend, that explains localStorage in detail?Aerobe
@Aerobe MDN: Storage and W3c: Web Storage.Revolution
well, even after reading the MDN page I still can't find the answer to my question... Anyway, thanks again!Aerobe
@Aerobe I have verified my statements a while back by viewing the sqlite(3) database called webappsstore.sqlite in my Firefox profile directory, using query SELECT scope FROM webappsstore2;. The result is the reverse of the domain, followed by the non-reversed protocol, and sufficed with the port, eg: gro.allizom.snodda.secivres.:https:443. As you can see, there's no mention of any path.Revolution
Here's documentation of the document.domain API mentioned: html.spec.whatwg.org/multipage/…Claman
The MDN: Web Storage API doc does now specify: "sessionStorage maintains a separate storage area for each given origin [...]", where the "origin" is as defined here: MDN: Origin - i.e. sessionStorage and localStorage are partitioned by scheme + hostname + portClaman
M
165

Session Storage:

  1. Values persist only as long as the window or tab in which they stored.

  2. Values are only visible within the window or tab that created them.

Local Storage:

  1. Values persist window and browser lifetimes.

  2. Values are shared across every window or tab running at the same origin.

So, by reading and understanding this each key-value pair is unique for each domain, because local storage persist values across window or tab.

Moselle answered 20/3, 2012 at 10:27 Comment(3)
Thanks. Could you share the link to this reference?Aerobe
Link above is now Defunct this is another great resource: sitepoint.com/an-overview-of-the-web-storage-apiArmandarmanda
The conclusion may be correct, but not the reasoning. Even if the storage were per-page, it could still persist across windows/tabs.Wallywalnut
R
105

The values are going to overwrite each other. Each key-name pair is unique for a protocol and domain, regardless of the paths.

The affected domain can be changed via the document.domain property.

  • sub.example.com -> example.com is possible (subdomain)
  • sub.example.com -> other.example.com is not possible

References:

  • MDN: Web Storage API: "sessionStorage maintains a separate storage area for each given origin [...]"
  • MDN: Origin: "Two objects have the same origin only when the scheme, hostname, and port all match."
Revolution answered 16/3, 2012 at 18:11 Comment(6)
Thanks! Would you have a reference to recommend, that explains localStorage in detail?Aerobe
@Aerobe MDN: Storage and W3c: Web Storage.Revolution
well, even after reading the MDN page I still can't find the answer to my question... Anyway, thanks again!Aerobe
@Aerobe I have verified my statements a while back by viewing the sqlite(3) database called webappsstore.sqlite in my Firefox profile directory, using query SELECT scope FROM webappsstore2;. The result is the reverse of the domain, followed by the non-reversed protocol, and sufficed with the port, eg: gro.allizom.snodda.secivres.:https:443. As you can see, there's no mention of any path.Revolution
Here's documentation of the document.domain API mentioned: html.spec.whatwg.org/multipage/…Claman
The MDN: Web Storage API doc does now specify: "sessionStorage maintains a separate storage area for each given origin [...]", where the "origin" is as defined here: MDN: Origin - i.e. sessionStorage and localStorage are partitioned by scheme + hostname + portClaman

© 2022 - 2024 — McMap. All rights reserved.