I'm trying out localStorage and JSON using a variety of browsers with this piece of code:
function getStorage() {
stored = JSON.parse(localStorage['test']);
if (typeof stored == 'object') {return stored;}
}
Chrome gives me the following error message: "Uncaught SyntaxError: Unexpected token u" for the first line inside the function body. For that same line, Safari says "SyntaxError: Unable to parse JSON string". Firefox does not give any error messages for that line.
Other JSON.parse examples (from books, etc) work fine in both Safari and Chrome. For instance, this one from "Pro JavaScript" by Nicholas Zakas:
var jsonText = "{\"name\":\"Nicholas C. Zakas\", \"age\":29, \"author\":true }";
var object = JSON.parse(jsonText);
alert(object.name); //"Nicholas C. Zakas"
No problem at all with getting that to work. Local storage also works fine in both.
I've also checked the encoding on all my files using file -I my file
and all files, both my own and the ones from the above book, are encoded as filename.html: text/html; charset=us-ascii
- so encoding doesn't seem to be the problem here.
This is perplexing. Thanks a lot for any help.
/ James