Created
August 1, 2012 18:37
-
-
Save noahadams/3229652 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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