aboutsummaryrefslogtreecommitdiff
path: root/files/id/web/api/storage/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/id/web/api/storage/index.html')
-rw-r--r--files/id/web/api/storage/index.html100
1 files changed, 100 insertions, 0 deletions
diff --git a/files/id/web/api/storage/index.html b/files/id/web/api/storage/index.html
new file mode 100644
index 0000000000..2231fe2bd8
--- /dev/null
+++ b/files/id/web/api/storage/index.html
@@ -0,0 +1,100 @@
+---
+title: Storage
+slug: Web/API/Storage
+translation_of: Web/API/Storage
+---
+<div>{{APIRef("Web Storage API")}}</div>
+
+<p>The <strong><code>Storage</code></strong> interface of the <a href="/en-US/docs/Web/API/Web_Storage_API">Web Storage API</a> 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 {{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 <code>n</code>, 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>{{domxref("Window.localStorage")}}</li>
+ <li>{{domxref("Window.sessionStorage")}}</li>
+ <li>{{domxref("CacheStorage")}}</li>
+</ul>