Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is sessionStorage actually cleared?

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)); }); 
like image 367
northernMonkey Avatar asked Apr 13 '16 16:04

northernMonkey


People also ask

Why does sessionStorage clear on refresh?

Introduction to JavaScript sessionStorage The sessionStorage object stores data only for a session. It means that the data stored in the sessionStorage will be deleted when the browser is closed. A page session lasts as long as the web browser is open and survives over the page refresh.

Does sessionStorage clear on browser close?

Use SessionStorage instead. It gets cleared off whenever the tab is closed.

How long is session storage accessible?

The sessionStorage object stores data for only one session. (The data is deleted when the browser is closed).

Is sessionStorage lost on refresh?

Web storage objects localStorage and sessionStorage allow to save key/value pairs in the browser. What's interesting about them is that the data survives a page refresh (for sessionStorage ) and even a full browser restart (for localStorage ).


1 Answers

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

  • Whenever a document is loaded in a particular tab in the browser, a unique page session gets created and assigned to that particular tab. That page session is valid only for that particular tab.
  • A page session lasts as long as the tab or the browser is open, and survives over page reloads and restores.
  • Opening a page in a new tab or window creates a new session with the value of the top-level browsing context, which differs from how session cookies work.
  • Opening multiple tabs/windows with the same URL creates sessionStorage for each tab/window.
  • Duplicating a tab copies the tab's sessionStorage into the new tab.
  • Closing a tab/window ends the session and clears objects in sessionStorage.

Note that the duplication of the tab does not seem to work in Firefox, it does however in Chrome.

like image 108
timolawl Avatar answered Oct 02 '22 16:10

timolawl