Web technology for developersSee Web APIsStorage
TheStorage
interface of theWeb Storage APIprovides access to a particular domain's session or local storage. It allows, for example, the addition, modification, or deletion of stored data items.
To manipulate, for instance, the session storage for a domain, a call toWindow.sessionStorage
is made; whereas for local storage the call is made toWindow.localStorage
.
Window.sessionStorage
ThesessionStorage
property accesses a sessionStorage
object for the current origin.sessionStorage
is similar tolocalStorage
; the difference is that while data inlocalStorage
doesn't expire, data insessionStorage
is cleared when thepage sessionends.
- A page session lasts as long as 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. - Closing a tab/window ends the session and clears objects in
sessionStorage
Window.localStorage
The read-onlylocalStorage
property allows you to access aStorage
object for theDocument
's origin; the stored data is saved across browser sessions.localStorage
is similar tosessionStorage
, except that while data stored inlocalStorage
has no expiration time, data stored insessionStorage
gets cleared when the page session ends — that is, when the page is closed. (Data in alocalStorage
object created in a "private browsing" or "incognito" session is cleared when the last "private" tab is closed.)
Data stored in eitherlocalStorage
orsessionStorage
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 differentlocalStorage
object from the same site accessed with HTTPS (e.g.,https://example.com).
The keys and the values arealwaysin the UTF-16DOMStringformat, which uses two bytes per character. (As with objects, integer keys are automatically converted to strings.)