Web Storage API の Storage インターフェイスは、特定のドメインのセッション ストレージまたはローカル ストレージへのアクセス機能を提供して、例えば保存されているデータ アイテムを追加、変更、削除することができます。
ドメインのセッション ストレージを操作したい場合は、window.sessionStorage() メソッドを呼び出してください。ドメインのローカル ストレージを操作したい場合は、window.localStorage() を呼び出してください。
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); }
注意: 完全に動作する例として実行する様子を見るために、Web Storage Demo をご覧ください。
表示内容: |
表示内容: |