javascript - JSON.parse() from localStorage issue -


i working on little browser game , using html localstorage save data.

the problem: have empty array later .push() data into. storing array in localstorage when try read local storage doesn't work.

the chrome developer tools console giving me error: "uncaught syntaxerror: unexpected token u" when trying parse data localstorage.

here's code using:

var allcontracts = []; localstorage["allcontracts"] = json.stringify(allcontracts);  allcontracts = json.parse(localstorage["allcontracts"]); 

there more code none of interacting these in way.

is there quirk localstorage or json not aware of , causing this? (i not familiar json or localstorage) should doing different way? or missing obvious mistake?

thanks in advance :)

the best way use methods interface of localstorage serves you. have setitem() , getitem() methods, why not use safe yourself?

var allcontracts = []; // setter localstorage.setitem("allcontracts",  json.stringify(allcontracts)); //getter var allcontracts = json.parse(localstorage.getitem("allcontracts")); 

with piece of code, overriding localstorage global object own values, lost functionality.

you make this:

localstorage = [] // transform default localstorage array 

and need this:

localstorage.setitem(key, value) 

more info: https://developer.mozilla.org/en-us/docs/web/api/window/localstorage


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -