Storage

Storage interface of the Web 存储 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 Window.sessionStorage is made; whereas for local storage the call is made to Window.localStorage .

特性

Storage.length 只读
Returns an integer representing the number of data items stored in the Storage 对象。

方法

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.

范例

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();
} 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);
}
					

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

规范

规范 状态 Comment
HTML Living Standard
The definition of 'Storage' in that specification.
实时标准

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
Storage Chrome 4 Edge 12 Firefox 3.5 IE 8 Opera 10.5 Safari 4 WebView Android ≤37 Chrome Android 18 Firefox Android 6 Opera Android 11 Safari iOS 3.2 Samsung Internet Android 1.0
clear Chrome 4 Edge 12 Firefox 3.5 IE 8 Opera 10.5 Safari 4 WebView Android Yes Chrome Android 18 Firefox Android 6 Opera Android 11 Safari iOS 3.2 Samsung Internet Android 1.0
getItem Chrome 4 Edge 12 Firefox 3.5 IE 8 Opera 10.5 Safari 4 WebView Android ≤37 Chrome Android 18 Firefox Android 6 Opera Android 11 Safari iOS 3.2 Samsung Internet Android 1.0
key Chrome 4 Edge 12 Firefox 3.5 IE 8 Opera 10.5 Safari 4 WebView Android Yes Chrome Android 18 Firefox Android 6 Opera Android 11 Safari iOS 3.2 Samsung Internet Android 1.0
length Chrome 4 Edge 12 Firefox 3.5 IE 8 Opera 10.5 Safari 4 WebView Android Yes Chrome Android 18 Firefox Android 6 Opera Android 11 Safari iOS 3.2 Samsung Internet Android 1.0
removeItem Chrome 4 Edge 12 Firefox 3.5 IE 8 Opera 10.5 Safari 4 WebView Android ≤37 Chrome Android 18 Firefox Android 6 Opera Android 11 Safari iOS 3.2 Samsung Internet Android 1.0
setItem Chrome 4 Edge 12 Firefox 3.5 IE 8 Opera 10.5 Safari 4 WebView Android ≤37 Chrome Android 18 Firefox Android 6 Opera Android 11 Safari iOS 3.2 Samsung Internet Android 1.0

图例

完整支持
完整支持

另请参阅