When is sessionStorage actually cleared?

northernMonkey picture northernMonkey · Apr 13, 2016 · Viewed 31.8k times · Source

I have some javascript that checks for an object in sessionStorage, and uses it to refill an input field. I use this to help users on my site if they leave the form unfinished and either navigate away or try to submit the form after their session has expired.

My understanding is that sessionStorage is NOT linked to a server session, it is linked to the browser, so whether I have a new session on the server or not is irrelevent.

This was supported when I was testing this initially a few months ago. However, it seems ot no longer be the case, and when I clear my session cookie and reload my page, my sessionStorage is also cleared out. This is using both Chrome and Firefox.

I don't want to use localStorage as that could cause issues with shared computers, whereas sessionStorage will be wiped out when the browser windows is closed.

JS to get the value of my stored object

JSON.parse(sessionStorage.getItem("draftPost") || null);

JS to save the value

$("#wallText").on("change",function(){
            sessionStorage.setItem("draftPost",JSON.stringify(draftPost));
        });

Answer

timolawl picture timolawl · Apr 13, 2016

Session storage is cleared when the browser closes. It persists over page reloads and restores. See: https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage