108

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?

jdphenix
  • 14,226
  • 3
  • 39
  • 70
Christophe
  • 25,975
  • 25
  • 91
  • 136

2 Answers2

158

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.

Talha
  • 18,147
  • 8
  • 47
  • 64
98

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
TRiG
  • 9,687
  • 6
  • 54
  • 105
Rob W
  • 328,606
  • 78
  • 779
  • 666
  • Thanks! Would you have a reference to recommend, that explains localStorage in detail? – Christophe Mar 16 '12 at 18:27
  • 2
    @Christophe [MDN: Storage](https://developer.mozilla.org/en/DOM/Storage) and [W3c: Web Storage](http://dev.w3.org/html5/webstorage/). – Rob W Mar 16 '12 at 18:30
  • 1
    well, even after reading the MDN page I still can't find the answer to my question... Anyway, thanks again! – Christophe Mar 16 '12 at 18:37
  • 1
    @Christophe 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. – Rob W Mar 16 '12 at 18:44
  • 1
    Here's documentation of the `document.domain` API mentioned: https://html.spec.whatwg.org/multipage/browsers.html#relaxing-the-same-origin-restriction – mltsy Feb 10 '17 at 15:40