Storage

Esta tradução está incompleta. Ajude atraduzir este artigo.

A interface de Armazenamento da Web Storage API fornece acesso ao armazenamento de sessão ou armazenamento local para um domínio específico, permitindo que você, por exemplo, adicione, modifique ou exclua itens de dados armazenados.

Se você quiser manipular o armazenamento de sessão para um domínio, você chama o método Window.sessionStorage; Se você quiser manipular o armazenamento local para um domínio, você chama Window.localStorage.

Propriedades

Storage.length Somente leitura
Returns an integer representing the number of data items stored in the Storage object.

Métodos

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

Exemplos

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')) {
  populateStorage();
} else {
  setStyles();
}

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

  setStyles();
}

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;

  htmlElem.style.backgroundColor = '#' + currentColor;
  pElem.style.fontFamily = currentFont;
  imgElem.setAttribute('src', currentImage);
}

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

Especificações

Specification Status Comment
Web Storage (Second edition)
The definition of 'Storage' in that specification.
Recommendation  

Compatibilidade com navegadores

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 3.2[1]

[1] 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.

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

Veja também

Using the Web Storage API

Etiquetas do documento e colaboradores

 Colaboradores desta página: andrekutianski, Sebastianz
 Última atualização por: andrekutianski,