LocalStorage is a way to store persistent data using JavaScript (see also: SessionStorage).
LocalStorage is a way to store persistent data using JavaScript. It should be used only with HTML5 compatible web browser. Only plain-text values can be stored. Arrays, hashes, numbers, strings and booleans can be stored by using JSON.stringify(value)
. Then, to get the original value when reading the value, use JSON.parse(stringified_value)
.
Key-value pairs in localStorage
can only be read by client-side code from the same domain.
Example:
localStorage.setItem('key', 'value');
alert(localStorage.getItem('key')); // Shows "value"
localStorage.removeItem('key');
It is recommended to not use localStorage['key_name'] = 'value';
to assign a variable. Otherwise, property conflicts can occur. For example, imagine what can happen if you want to store a preference called 'setItem'
.