Thursday, November 27, 2014


One of those things I grew up living with out (because it didn't exist in most browsers) and so don't always think of: localStorage is trivial to use in modern browsers, and provides decent support when you want to store some state across sessions, but don't want to make up a backend server to support it.

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.
and later
or you can even use array-like syntax:

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));
someThing = JSON.parse(localStorage.getItem("someKey"));
(with an appropriate safety checks)

No comments:

Post a Comment