--- title: Storage.getItem() slug: Web/API/Storage/getItem tags: - API - Method - Reference - Storage - Web Storage translation_of: Web/API/Storage/getItem ---
{{APIRef("Web Storage API")}}
{{domxref("Storage")}} インターフェイスの getItem()
メソッドはキーの名称を渡すと、そのキーに対する値を返します。
var aValue = storage.getItem(keyName);
keyName
キーに対する値を持つ {{domxref("DOMString")}}。キーが存在しない場合は null
が返ります。
以下の関数はローカルストレージから 3 個のデータアイテムを取り出して、その値を使用してページのカスタムスタイルを設定します。
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 をご覧ください。
仕様書 | 策定状況 | コメント |
---|---|---|
{{SpecName('Web Storage', '#dom-storage-getitem', 'getItem()')}} | {{Spec2('Web Storage')}} |
機能 | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
localStorage | 4 | 3.5 | 8 | 10.50 | 4 |
sessionStorage | 5 | 2 | 8 | 10.50 | 4 |
機能 | Android | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
基本サポート | 2.1 | {{CompatUnknown}} | 8 | 11 | iOS 3.2 |
すべてのブラウザで、localStorage および sessionStorage が受け入れる容量は異なります。さまざまなブラウザのストレージ容量を報告しているページがあります。
注記: iOS 5.1 より Safari Mobile は localStorage データを cache フォルダに保存しており、概して空き容量が少ない場合に OS の要求により、時々クリーンアップを受けます。