Mozilla wants to hear from developers like you.


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

L'interface Storage de l'API Web Storage donne accès au session storage ou au local storage, pour un domaine donné. Elle permet, par exemple d'ajouter, de modifier ou supprimer des éléments d'information.

Manipuler le session storage pour un domaine se fait à l'aide de WindowSessionStorage.sessionStorage tandis que manipuler le local storage pour un domaine se fait à l'aide de WindowLocalStorage.localStorage.


Storage.length Lecture seule
Renvoie un entier représentant le nombre d'éléments contenus dans l'objet Storage.


When passed a number n, this method will return the name of the nth key in the storage.
When passed a key name, will return that key's value.
When passed a key name and value, will add that key to the storage, or update that key's value if it already exists.
When passed a key name, will remove that key from the storage.
When invoked, will empty all keys out of the storage.


Here we access a Storage object by calling localStorage. We first test whether the local storage contains data items using !localStorage.getItem('bgcolor'). If it does, we run a function called setStyles() that grabs the data items using localStorage.getItem() and uses those values to update page styles. If it doesn't, we run another function, populateStorage(), which uses localStorage.setItem() to set the item values, then runs setStyles().

if(!localStorage.getItem('bgcolor')) {
} else {

function populateStorage() {
  localStorage.setItem('bgcolor', document.getElementById('bgcolor').value);
  localStorage.setItem('font', document.getElementById('font').value);
  localStorage.setItem('image', document.getElementById('image').value);


function setStyles() {
  var currentColor = localStorage.getItem('bgcolor');
  var currentFont = localStorage.getItem('font');
  var currentImage = localStorage.getItem('image');

  document.getElementById('bgcolor').value = currentColor;
  document.getElementById('font').value = currentFont;
  document.getElementById('image').value = currentImage; = '#' + currentColor; = currentFont;
  imgElem.setAttribute('src', currentImage);

Note: To see this running as a complete working example, see our Web Storage Demo.


Specification Status Comment
Web Storage
La définition de 'Storage' dans cette spécification.

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.

See also

Using the Web Storage API

Étiquettes et contributeurs liés au document

 Contributeurs à cette page : gpenissard
 Dernière mise à jour par : gpenissard,