Scope of sessionStorage and localStorage

JavascriptHtmlLocal StorageSessionstorage

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChristopheView Question on Stackoverflow
Solution 1 - JavascriptTalhaView Answer on Stackoverflow
Solution 2 - JavascriptRob WView Answer on Stackoverflow