Friday, April 29, 2016

Scope of sessionStorage and localStorage

Scope of sessionStorage and localStorage

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.domain.com -> domain.com is possible (subdomain)
sub.domain.com -> other.domain.com is not possible

Session Storage:

Values persist only as long as the window or tab in which they stored.
Values are only visible within the window or tab that created them.

Local Storage:

Values persist window and browser lifetimes.
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.

  localStorage.setItem("test", "Hello World");

  var test = localStorage.getItem("test");
  console.log(test);

  for(var i in window.localStorage) {
    console.log(i + ": " + window.localStorage[i]);
  }

Reference:

http://stackoverflow.com/questions/9742395/scope-of-sessionstorage-and-localstorage

http://stackoverflow.com/questions/4201239/in-html5-is-the-localstorage-object-isolated-per-page-domain

No comments: