developerasun / myCodeBox-web

Open source code box for web developers.
Apache License 2.0
5 stars 0 forks source link

Javascript : localStorage vs sessionStorage #202

Open developerasun opened 2 years ago

developerasun commented 2 years ago

research : understanding difference between localStorage and sessionStorage

read this

localStorage

The localStorage read-only property of the window interface allows you to access a Storage object for the Document's origin; the stored data is saved across browser sessions.

localStorage is similar to sessionStorage, except that while localStorage data has no expiration time, sessionStorage data gets cleared when the page session ends — that is, when the page is closed. (localStorage data for a document loaded in a "private browsing" or "incognito" session is cleared when the last "private" tab is closed.)

sessionStorage

The read-only sessionStorage property accesses a session Storage object for the current origin. sessionStorage is similar to localStorage; the difference is that while data in localStorage doesn't expire, data in sessionStorage is cleared when the page session ends.

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. Data stored in sessionStorage is specific to the protocol of the page. In particular, data stored by a script on a site accessed with HTTP (e.g., http://example.com) is put in a different sessionStorage object from the same site accessed with HTTPS (e.g., https://example.com/).

reference