jquery - Making an Array Variable Global in JavaScript -
i'm setting array variable 0 on load of javascript.
var postarray= new array();
however, used array hold objects retrieve localstorage. works when upload more blog posts entered localstorage , displays. however, after refresh page called again empty array , go enter 3rd blog post after i've refreshed, localstorage set having contents of postarray. therefore, i'm clearing out localstorage when dont want to.
if throw if statement, postarray undefined.
if (localstorage.getitem("posts") === null) { var postarray = new array(); }
i'm trying make postarray global @ start yet create new array when localstorage empty.
you should content localstorage, , if it's empty, return empty array.
for example.
var postarray= getposts(); function getposts() { var posts = localstorage.getitem("posts"); return posts ? json.parse(posts) : []; }
Comments
Post a Comment