A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://webplatform.github.io/docs/apis/web-storage/Storage/localStorage below:

localStorage · WebPlatform Docs

localStorage Summary

Provides a Storage object for an origin, that remains persistent even after restarting the browser. The storage can be cleared by the user with browser functionalities. If you need a temporary storage, use apis/web-storage/Storage/sessionStorage

Property of apis/web-storage/Storageapis/web-storage/Storage

Syntax

Note: This property is read-only.

var result = object.localStorage;
Return Value

Returns an object of type ObjectObject

Examples
if (window.localStorage) { 
  if (localStorage['testKey']) { 
    console.log('Value exist on page load in localstorage for key testKey : ', localStorage['testKey']);
  }
  localStorage['testKey'] = 'Hi again!'; 
}
else {
 console.log('your browser dont support localstorage');
}

Fields and buttons for saving, reading and clearing localStorage items.

<section>
    <label for="key">Key:</label>
    <input type="text" id="key" value="r2d2">
    <br>
    <label for="value">Value:</label>
    <input type="text" id="value" value="C-3PO">
    <br>
    <button type="button" id="set-local">Save to localStorage</button>
</section>
<hr>
<section>
    <label for="get-key">Key:</label>
    <input type="text" id="get-key" value="r2d2">
    <button type="button" id="get-local">Get from localStorage</button>
    <output id="output"></output>
</section>
<hr>
<section>
    <button type="button" id="clear">Clear localStorage</button>
</section>

View live example

Functions and event handlers for saving, reading and clearing localStorage items.


var valueSetHandler = function () {
    
    var key = document.getElementById('key').value,
        value = document.getElementById('value').value;

    
    window.localStorage.setItem(key, value);
},
valueGetHandler = function () {
    
    var key = document.getElementById('get-key').value,

        
        value = window.localStorage.getItem(key);

    
    document.getElementById('output').innerText = value;
},
clearStorageHandler = function () {
    window.localStorage.clear();
};


document.getElementById('set-local').addEventListener('click', valueSetHandler);
document.getElementById('get-local').addEventListener('click', valueGetHandler);
document.getElementById('clear').addEventListener('click', clearStorageHandler);
Usage
 Use via the methods setItem, getItem, removeItem and clear provided by apis/web-storage/Storage.

Listen to the storage event on dom/Window to catch changes in the storage (example: http://jsfiddle.net/A6tuM/1/).

Notes

The localStorage “property” provides an instance of a storage area object, to which the Storage object’s properties and methods are applied.

The amount of storage is limited by the browser on a per location basis (e.g. per domain). An error message is thrown, when the quota is exceed.

See for an example of the error message here (the example shows the error message for when the sessionStorage quota is exceeded. The behavior is similar for localStorage): http://jsfiddle.net/wkDc6/1/

Related specifications
W3C Web Storage Specification
W3C Recommendation
See also Related articles Off-line Storage Attributions

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4