localStorage.getItem()
פונקציית ()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 |
---|---|---|
HTML Living Standard The definition of 'Storage.getItem' in that specification. |
Living Standard |
תאימות דפדפן
BCD tables only load in the browser