Last Updated: February 25, 2016
·
682
· diogomachado

Working with localStorage

Now, we go talking as writing simple code for working with localStorage.

Setting one value for key

window.localStorage.setItem("key", "value");

Getting a value for key stored

window.localStorage.getItem("key", "value");

Remove one item specific

window.localStorage.removeItem("key");

Clear all keys storages in your browser

window.localStorage.clear();

You can require storing a array in your key,but the API localStorage don't work with array, but exist one simple solution for this:

Create a array

var items = [];

Stored you array with function JSON.stringify()

localStorage["key"] = JSON.stringify(items);

Get you array and modify values with JSON.parse

var storedItems = JSON.parse(localStorage["key"]);

Insert a value in array existing

storedItems.push(item);

Now, you know writing web applications, use your imagination for create amazing apps. The support for this tecnology is very good, you can see in website: http://caniuse.com/namevalue-storage

Thanks!