--- title: localStorage.getItem() slug: Web/API/Storage/getItem translation_of: Web/API/Storage/getItem ---
{{APIRef("Web Storage API")}}
פונקציית ()getItem
מאפשרת לאחזר נתונים מהאחסון המקומי באמצעות שם המפתח.
במידה והמפתח שרוצים לאחזר אינו קיים באחסון המקומי, יוחזר הערך null.
var aValue = localStorage.getItem(keyName);
keyName
במידה והמפתח קיים באחסון המקומי הוא יחזיר את ערכו. במידה והמפתח אינו קיים יוחזר הערך null.
הפונקציה הבאה מאחזרת שלושה פריטי נתונים מהאחסון המקומי ולאחר מכן משתמשת בהם כדי להגדיר גיליונות סגנון מדורגים.
function setStyles() { var currentColor = localStorage.getItem('bgcolor'); var currentFont = localStorage.getItem('font'); var currentImage = localStorage.getItem('image'); currentColor = document.getElementById('bgcolor').value; currentFont = document.getElementById('font').value; currentImage = document.getElementById('image').value; htmlElem.style.backgroundColor = '#' + currentColor; pElem.style.fontFamily = currentFont; imgElem.setAttribute('src', currentImage); }
Specification | Status | Comment |
---|---|---|
{{SpecName('HTML WHATWG', 'webstorage.html#dom-storage-getitem', 'Storage.getItem')}} | {{Spec2('HTML WHATWG')}} |
{{Compat("api.Storage.getItem")}}