1

I'm trying to set a variable on one page and then display it on another page. Both pages exist on the same domain. I've never used sessionStorage before so I'm not really sure where I made my mistake. The second page is just a blank page for some reason. Here is the code on the first page that sets the variable.

if (typeof(Storage) != "undefined") {
    // Store
    sessionStorage.setItem("score", 12);
    document.getElementById("result").innerHTML = ("score");
} else {
    document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Storage...";
}   

And here is the code on the second page that is supposed to retrieve the variable and print it to the screen.

if (typeof(Storage) != "undefined") {
    // Retrieve
    document.getElementById("result").innerHTML = sessionStorage.getItem("score");
} else {
    document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Storage...";
}

Any insight on why this isn't getting the variable would be great thanks.

Ben Black
  • 3,751
  • 2
  • 25
  • 43
datdudebp
  • 13
  • 1
  • 1
  • 3
  • Some read about when to use sessionStorage and when to use localStorage [here](http://stackoverflow.com/questions/5523140/html5-local-storage-vs-session-storage) and [here](http://stackoverflow.com/questions/19867599/what-is-the-difference-between-localstorage-sessionstorage-session-and-cookie) – Alexandru Chichinete Jul 28 '15 at 14:54

4 Answers4

5

As Yiriy pointed out, localStorage should be used instead of sessionStorage when trying to share a variable between multiple windows/tabs of the browser.

So use:

localStorage.setItem("score",12);

and

document.getElementById("result").innerHTML = localStorage.getItem("score");

in the other tab

Just think of sessionStorage stores local variables while localStorage stores global variables.

Huan Zhang
  • 1,095
  • 8
  • 13
4

"...sessionStorage gets cleared when the page session ends"

Use localStorage instead.

Yuriy Galanter
  • 38,833
  • 15
  • 69
  • 136
1

As in sessionStorage description:

  • it gets cleared when the page session ends
  • opening a page in a new tab or window will cause a new session to be initiated

Use localStorage instead

tkeram
  • 214
  • 1
  • 5
  • 1
    I actually originally used localStorage, but the same thing happened, a blank screen on the second page. Any idea why? – datdudebp Jul 28 '15 at 15:04
1

When a user opens a new tab, we first ask any other tab that is opened if he already have the sessionStorage for us. If any other tab is opened it’ll send us the sessionStorage through localStorage event, we’ll duplicate that into the sessionStorage.

// on opening the new tab, we check the session storage. 
// if it's empty, we send (from the new tab) an localStorage event, with particular key(getSessionStorage), by setting some value in localStorage
if (!sessionStorage.length) {
  localStorage.setItem('getSessionStorage', Date.now()); // send Event1 from new tab
};

window.addEventListener('storage', function(event) {
  // in first tab, we listen for the "getSessionStorage" event and set the sessionStorage info in the localStorage.
  if (event.key == 'getSessionStorage') { // Event1 captured in first tab
    localStorage.setItem('sessionStorage', JSON.stringify(sessionStorage)); // emit Event2 from first tab. this contains session storage info.
    localStorage.removeItem('sessionStorage');
  } else if (event.key == 'sessionStorage' && !sessionStorage.length) { // capture Event2 in new tab. read the event value and set in session storage in new tab
    var data = JSON.parse(event.newValue);
    for (key in data) {
      sessionStorage.setItem(key, data[key]);
    }
  }
});

source - https://blog.guya.net/2015/06/12/sharing-sessionstorage-between-tabs-for-secure-multi-tab-authentication/

Venugopal
  • 1,888
  • 1
  • 17
  • 30