1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
---
title: Storage
slug: Web/API/Storage
tags:
- API
- Interface
- NeedsTranslation
- Reference
- Storage
- TopicStub
- Web Storage
- data
translation_of: Web/API/Storage
---
<p>{{APIRef("Web Storage API")}}</p>
<p>The <code>Storage</code> interface of the Web Storage API provides access to a particular domain's session or local storage. It allows, for example, the addition, modification, or deletion of stored data items.</p>
<p>To manipulate, for instance, the session storage for a domain, a call to the {{domxref("Window.sessionStorage")}} is made; whereas for local storage the call is made to {{domxref("Window.localStorage")}}.</p>
<h2 id="Properties">Properties</h2>
<dl>
<dt>{{domxref("Storage.length")}} {{readonlyInline}}</dt>
<dd>Returns an integer representing the number of data items stored in the <code>Storage</code> object.</dd>
</dl>
<h2 id="Methods">Methods</h2>
<dl>
<dt>{{domxref("Storage.key()")}}</dt>
<dd>When passed a number n, this method will return the name of the nth key in the storage.</dd>
</dl>
<dl>
<dt>{{domxref("Storage.getItem()")}}</dt>
<dd>When passed a key name, will return that key's value.</dd>
<dt>{{domxref("Storage.setItem()")}}</dt>
<dd>When passed a key name and value, will add that key to the storage, or update that key's value if it already exists.</dd>
<dt>{{domxref("Storage.removeItem()")}}</dt>
<dd>When passed a key name, will remove that key from the storage.</dd>
<dt>{{domxref("Storage.clear()")}}</dt>
<dd>When invoked, will empty all keys out of the storage.</dd>
</dl>
<h2 id="Examples">Examples</h2>
<p>Here we access a <code>Storage</code> object by calling <code>localStorage</code>. We first test whether the local storage contains data items using <code>!localStorage.getItem('bgcolor')</code>. If it does, we run a function called <code>setStyles()</code> that grabs the data items using {{domxref("Storage.getItem()")}} and uses those values to update page styles. If it doesn't, we run another function, <code>populateStorage()</code>, which uses {{domxref("Storage.setItem()")}} to set the item values, then runs <code>setStyles()</code>.</p>
<pre class="brush: js">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);
}</pre>
<div class="note">
<p><strong>Note</strong>: To see this running as a complete working example, see our <a href="https://mdn.github.io/dom-examples/web-storage/">Web Storage Demo</a>.</p>
</div>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('HTML WHATWG', 'webstorage.html#the-storage-interface', 'Storage')}}</td>
<td>{{Spec2('HTML WHATWG')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat("api.Storage")}}</p>
<h2 id="See_also">See also</h2>
<ul>
<li><a href="/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API">Using the Web Storage API</a></li>
<li><a href="/en-US/docs/Web/API/Window/localStorage">Window.localStorage</a></li>
</ul>
|