Sunday, March 25, 2012

everything you stick in localStorage ends up as a string

You will thus need to cast integers back into integer form like so:

foo = parseInt(localStorage["bar"]);

 
 

Also, if you need to store a complicated object, this suggests that you stringify JSON to shove the object(s) in and then parse the string back to JSON when retrieving like so:

var testObject = { 'one': 1, 'two': 2, 'three': 3 };
localStorage.setItem('testObject', JSON.stringify(testObject));
var retrievedObject = localStorage.getItem('testObject');
console.log('retrievedObject: ', JSON.parse(retrievedObject));

No comments:

Post a Comment