Web Storage API

Cette traduction est incomplète. Aidez à traduire cet article depuis l'anglais.

L'API Web Storage fournit des mécanismes par lesquels les navigateurs peuvent stocker des paires clé / valeur, d'une manière beaucoup plus intuitive que l'utilisation de cookies.

Web Storage, concepts et utilisations

Les deux mécanismes au sein du web storage sont les suivantes:

  • sessionStorage maintient une zone de stockage distinct pour chaque origine donnée qui est disponible pour la durée de la session de la page (tant que le navigateur est ouvert, y compris les rechargements et restaure)
  • localStorage fait la même chose, mais persiste même lorsque le navigateur est fermé et rouvert.

Ces mécanismes sont disponibles via les propriétés Window.sessionStorage et Window.localStorage (pour être plus précis, pour le support des navigateurs, l'objet Window implemente le WindowLocalStorage et l'object WindowSessionStorage, dont les propriétés localStorage and sessionStorage dépendent) — L'appel de l'une de ces propriétés va créer une instance de l'objet Storage,  à travers duquel les éléments de données peuvent être définis, récupérés et éliminés. Un objet de stockage différent est utilisé pour le sessionStorage et le localStorage pour chaque origine — ils fonctionnent et sont contrôlés séparément.

Note: Access to Web Storage from third-party IFrames is denied if the user has disabled third-party cookies (Firefox implements this behaviour from version 43 onwards.)

Note: Web Storage is not the same as mozStorage (Mozilla's XPCOM interfaces to SQLite) or the Session store API (an XPCOM storage utility for use by extensions).

Web Storage interfaces

Storage
Allows you to set, retrieve and remove data for a specific domain and storage type (session or local.)
Window
The Web Storage API extends the Window object with two new properties — Window.sessionStorage and Window.localStorage — which provide access to the current domain's session and local Storage objects respectively.
StorageEvent
The storage event is fired on a Document's Window object when a storage area changes.

Examples

To illustrate some typical web storage usage, we have created a simple example, imaginatively called Web Storage Demo. The landing page provides controls that can be used to customize the colour, font and decorative image. When you choose different options, the page is instantly updated; in addition the your choices are stored in localStorage,  so that when you leave the page then load it again later on your choices are remembered.

In addition, we have provided an event output page — if you load this page in another tab, then make changes to your choices in the landing page, you'll see the updated storage information outputted as the StorageEvent is fired.

Specifications

Specification Status Comment
Web Storage (Second edition) Recommendation  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
localStorage 4 3.5 8 10.50 4
sessionStorage 5 2 8 10.50 4
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support 2.1 ? 8 11 iOS 3.2

All browsers have varying capacity levels for both localStorage and sessionStorage. Here is a detailed rundown of all the storage capacities for various browsers.

Note: since iOS 5.1, Safari Mobile stores localStorage data in the cache folder, which is subject to occasional clean up, at the behest of the OS, typically if space is short.

Private Browsing / Incognito modes

Most browsers these days support a privacy option called 'Incognito' or 'Private Browsing' mode etc that basically makes sure that the private browsing session leaves no traces after the browser is closed. This is fundamentally incompatible with Web Storage for obvious reasons. As such, browser vendors are experimenting with different scenarios for how to deal with this incompatibility.

Most browsers have opted for a strategy where storage APIs are still available and seemingly fully functional, with the one big difference that all stored data is wiped after the browser is closed. For these browsers there are still different interpretations of what should be done with existing stored data (from a regular browsing session). Should it be available to read when in Private mode? Then there are some browsers, most notably Safari, that have opted for a solution where storage is available, but is empty and has a quota of 0 bytes assigned, effectively making it impossible to write data to it.

Developers should be aware of these different implementations and take them into account when developing websites depending on Web Storage APIs. For more information please have a look at this WHATWG blog post that specifically deals with this topic.

See also

Using the Web Storage API

Étiquettes et contributeurs liés au document

 Contributeurs à cette page : necraidan, Dexter_Deter, teoli
 Dernière mise à jour par : necraidan,