Skip to content

Instantly share code, notes, and snippets.

@catkins
Created November 16, 2014 05:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save catkins/d507d2196fb929b4e2e7 to your computer and use it in GitHub Desktop.
Save catkins/d507d2196fb929b4e2e7 to your computer and use it in GitHub Desktop.
Local Storage with JSON support
function saveItem(key, value) {
var json = JSON.stringify(value);
localStorage.setItem(key, json);
}
function loadItem(key) {
var json = localStorage.getItem(key);
return JSON.parse(json);
}
saveItem('numbers', [1, 2, 3]);
loadItem('numbers') // => [1, 2, 3]
saveItem('myHash', { hello: 'world' });
loadItem('myHash') // => { hello: "world" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment