Question:
b' I\'d like to store a JavaScript object in HTML5 localStorage, but my object is apparently being converted to a string.I can store and retrieve primitive JavaScript types and arrays using localStorage, but objects don\'t seem to work. Should they?Here\'s my code:var testObject = { \'one\': 1, \'two\': 2, \'three\': 3 };console.log(\'typeof testObject: \' + typeof testObject);console.log(\'testObject properties:\');for (var prop in testObject) { console.log(\' \' + prop + \': \' + testObject[prop]);}// Put the object into storagelocalStorage.setItem(\'testObject\', testObject);// Retrieve the object from storagevar retrievedObject = localStorage.getItem(\'testObject\');console.log(\'typeof retrievedObject: \' + typeof retrievedObject);console.log(\'Value of retrievedObject: \' + retrievedObject);The console output istypeof testObject: objecttestObject properties: one: 1 two: 2 three: 3typeof retrievedObject: stringValue of retrievedObject: [object Object]It looks to me like the setItem method is converting the input to a string before storing it.I see this behavior in Safari, Chrome, and Firefox, so I assume it\'s my misunderstanding of the HTML5 Web Storage specification, not a browser-specific bug or limitation.I\'ve tried to make sense of the structured clone algorithm described in 2 Common infrastructure. I don\'t fully understand what it\'s saying, but maybe my problem has to do with my object\'s properties not being enumerable (???).Is there an easy workaround?Update: The W3C eventually changed their minds about the structured-clone specification, and decided to change the spec to match the implementations. See 12111 \xe2\x80\x93 spec for Storage object getItem(key) method does not match implementation behavior. So this question is no longer 100% valid, but the answers still may be of interest. '
Solution : https://worcraft-algeria-dz.com/howto/64/javascript---How-to-store-objects-in-HTML5-localStorage/ | Source : https://worcraft-algeria-dz.com/