The Storage
interface of the Web Storage API provides 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 to the Window.sessionStorage
is made; whereas for local storage the call is made to Window.localStorage
.
Storage.length
Read only
Storage
object.Storage.key()
Storage.getItem()
Storage.setItem()
Storage.removeItem()
Storage.clear()
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 Storage.getItem()
and uses those values to update page styles. If it doesn't, we run another function, populateStorage()
, which uses Storage.setItem()
to set the item values, then runs setStyles()
.
if(!localStorage.getItem('bgcolor')) { populateStorage(); } setStyles(); 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; 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.
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'Storage' in that specification. | Living Standard |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 4 | 12 | 3.5 | 8 | 10.5 | 4 |
clear |
4 | 12 | 3.5 | 8 | 10.5 | 4 |
getItem |
4 | 12 | 3.5 | 8 | 10.5 | 4 |
key |
4 | 12 | 3.5 | 8 | 10.5 | 4 |
length |
4 | 12 | 3.5 | 8 | 10.5 | 4 |
removeItem |
4 | 12 | 3.5 | 8 | 10.5 | 4 |
setItem |
4 | 12 | 3.5 | 8 | 10.5 | 4 |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | 18 | 13 | 6 | 11 | 3.2 | ? |
clear |
Yes | 18 | 13 | 6 | 11 | 3.2 | ? |
getItem |
Yes | 18 | 13 | 6 | 11 | 3.2 | ? |
key |
Yes | 18 | 13 | 6 | 11 | 3.2 | ? |
length |
Yes | 18 | 13 | 6 | 11 | 3.2 | ? |
removeItem |
Yes | 18 | 13 | 6 | 11 | 3.2 | ? |
setItem |
Yes | 18 | 13 | 6 | 11 | 3.2 | ? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Storage