Using localStorage
suggest changeThe localStorage object stores strings for an origin. Other windows and frames of that origin see the new value the next time they read it. There is no expiration time. Data stays until it is removed, the user clears site data, or the browser evicts it. Writing can throw when storage is disabled or the quota is exceeded. Quotas differ by browser and are often about 5 MB per origin.
localStorage.setItem('name', "John Smith");
console.log(localStorage.getItem('name')); // "John Smith"
localStorage.removeItem('name');
console.log(localStorage.getItem('name')); // null
If you want to store simple structured data, you can use JSON to serialize it to and from strings for storage.
var players = [{name: "Tyler", score: 22}, {name: "Ryan", score: 41}];
localStorage.setItem('players', JSON.stringify(players));
console.log(JSON.parse(localStorage.getItem('players')));
// [ Object { name: "Tyler", score: 22 }, Object { name: "Ryan", score: 41 } ]
localStorage limits in browsers
Older tables listed a few megabytes for specific browser versions, with inconsistent units. Those figures are not a current support matrix. Test the quota in the browsers you support.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents