English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
getItem()Method returns the value of the specified key name in the given storage object.
If the specified key does not exist, this method returnsnull.
The getItem() method belongs to the storage object and can belocalStorageObject orsessionStorrageObject.
localStorage.getItem(keyName)
sessionStorage.getItem(keyName)
var x = localStorage.getItem("image");Test and See‹/›
The numbers in the table specify the first browser version that fully supports the getItem() method:
Method | |||||
getItem() | 4 | 3.5 | 11.5 | 4 | 9 |
Parameter | Description |
---|---|
keyName | A string that contains the name of the key to retrieve its value |
Return value: | A string containing the key value specified. If the specified key does not exist, it returns null |
---|---|
DOM Version: | Web Storage API |
Returns the value of the specified session storage item:
var x = sessionStorage.getItem("age");Test and See‹/›
You can also use dot notation to get values:
var x = localStorage.url;Test and See‹/›
The following functions retrieve three data items from local storage and then use them to set custom styles on the page:
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; root.style.backgroundColor = currentColor; p.style.fontFamily = currentFont; img.setAttribute(' src' currentImage); }Test and See‹/›
HTML Tutorial:Web Storage API
Window (Window) Reference:window.localStorage Property
Window (Window) Reference:window.sessionStorage Property