You have to be ok with not getting state back if the user is using a different device or browser, but then again, that's always true unless you've actually made some kind of server-side account system. (Which also has UX implications, in terms of a lot of people have "make an account to use this fatigue". And also "link to my FB account" fatigue.)
Anyway, window.localStorage; it's just sitting there, waiting for you, with old Old Skool messing with cookies.
localStorage.setItem("someKey",someThing);
and later
localStorage.getItem("someKey");
or you can even use array-like syntax:
localStorage["someKey"];
The only caveat it is many or all browsers will coerce your data to a string, so most likely you want something like
localStorage.setItem("someKey", JSON.stringify(someThing));
and
someThing = JSON.parse(localStorage.getItem("someKey"));
(with an appropriate safety checks)
No comments:
Post a Comment