The localStorage object provides persistent (but not permanent - see limits below) key-value storage of strings. Any changes are immediately visible in all other windows/frames from the same origin. The stored values persistent indefinitely unless the user clears saved data or configures an expiration limit. localStorage uses a map-like interface for getting and setting values.
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 } ]
Mobile browsers:
Browser | Google Chrome | Android Browser | Firefox | iOS Safari |
---|---|---|---|---|
Version | 40 | 4.3 | 34 | 6-8 |
Space available | 10MB | 2MB | 10MB | 5MB |
Desktop browsers:
Browser | Google Chrome | Opera | Firefox | Safari | Internet Explorer |
---|---|---|---|---|---|
Version | 40 | 27 | 34 | 6-8 | 9-11 |
Space available | 10MB | 10MB | 10MB | 5MB | 10MB |