By Christophe


2012-03-16 18:08:57 8 Comments

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?

2 comments

@Rob W 2012-03-16 18:11:43

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

@Christophe 2012-03-16 18:27:28

Thanks! Would you have a reference to recommend, that explains localStorage in detail?

@Rob W 2012-03-16 18:30:20

@Christophe MDN: Storage and W3c: Web Storage.

@Christophe 2012-03-16 18:37:58

well, even after reading the MDN page I still can't find the answer to my question... Anyway, thanks again!

@Rob W 2012-03-16 18:44:26

@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.

@mltsy 2017-02-10 15:40:14

Here's documentation of the document.domain API mentioned: html.spec.whatwg.org/multipage/…

@Talha 2012-03-20 10:27:49

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.

@Christophe 2012-03-20 15:33:04

Thanks. Could you share the link to this reference?

@chrisjlee 2013-12-26 20:21:40

Link above is now Defunct this is another great resource: sitepoint.com/an-overview-of-the-web-storage-api

@pyrocrasty 2015-07-18 20:25:27

The conclusion may be correct, but not the reasoning. Even if the storage were per-page, it could still persist across windows/tabs.

Related Questions

Sponsored Content

12 Answered Questions

[SOLVED] Working with $scope.$emit and $scope.$on

  • 2013-01-24 13:03:17
  • Paul Kononenko
  • 587164 View
  • 883 Score
  • 12 Answer
  • Tags:   javascript angularjs

25 Answered Questions

[SOLVED] What is the scope of variables in JavaScript?

17 Answered Questions

19 Answered Questions

[SOLVED] Storing Objects in HTML5 localStorage

9 Answered Questions

[SOLVED] Clearing localStorage in javascript?

7 Answered Questions

12 Answered Questions

[SOLVED] What is the max size of localStorage values?

1 Answered Questions

[SOLVED] sessionStorage not persisting between pages

1 Answered Questions

[SOLVED] localstorage in iframe of different domain using postMessage

Sponsored Content