Skip to content

Instantly share code, notes, and snippets.

@noahadams
Created August 1, 2012 18:37
Show Gist options
  • Save noahadams/3229652 to your computer and use it in GitHub Desktop.
Save noahadams/3229652 to your computer and use it in GitHub Desktop.
// A key to use for localStorage
var lsKey = "my very unique key"
// Some data to store in it, this could just as easily be from a web service
, dataStore = {
0: {
'name': 'Steven Patrick Morrissey',
'role': 'singer'
},
1: {
'name': 'Johnny Marr',
'role': 'guitarist'
}
}
// A JSON string serialization of the data
, serialized = JSON.stringify(dataStore);
// Persist it with localStorage
localStorage.setItem(lsKey, serialized);
// On a subsequent page load, restore the data
try {
dataStore = JSON.parse(localStorage.getItem(lsKey));
} catch(e) {
dataStore = {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment