- Cookies expire and get cleared a lot, localStorage is forever (until explicitly cleared).
- localStorage isn’t sent along in HTTP Requests, you have to ask for it.
- You can store way more data in localStorage than you can in cookies.
- The API for localStorage is wicked simple and easy.
If you need to get more up to speed, I did a video screencast on localStorage recently. This is how it works:
/* Set some data */ localStorage.setItem("key", "value"); /* Get some data */ localStorage.getItem("key");
OK great that’s all the geeky bits, let’s see some places on the web that could be, should be, or are using it. Hopefully this will spark some ideas for your own websites.
IS using localStorage
Snapbird (a powerful Twitter search tool) remembers the last person’s stream you searched. It figures, chances are, the next time you come back you’ll be searching that same person and this will save you a second. If not, you can just delete it and replace it. I find it useful as I’m typically trying to find old tweets of my own.
The Webbie Madness event this year had a large March Madness style voting grid where people were encouraged to come pick winners in multiple rounds. The first round had 32 people in 16 battles. The peoples names were disguised. It took a while to figure out who was who and decide who to vote for, and it wasn’t something I was able to do in one “sitting”. I suggested they save the votes as people did them to localStorage, that way you could come back any time and finish. Like total badasses, they did it!
SHOULD be using localStorage
Typekit has a Type Tester tool when viewing a particular font. You can type your own text into the box to see what it will look like in that font. Unfortunately when you go to another font, that custom text you entered is gone. This would be a great case for localStorage. When the value in that area changes, save it to localStorage, and load that value in when the page loads. I think that would be a great timesaver, as I think jumping around to see that text in multiple fonts is a pretty common use case.
CSS Lint has a grid of checkboxes for what features you do/don’t want to CSS Lint to look for when you test your CSS. It would be really nice if this used localStorage to remember what checkbox configuration you used, so if you reload the page or come back later, it’s the same. So if you don’t like the “Don’t use IDs” checkbox, you can turn if off forever =).
Pretty much any website with a login form could use localStorage to keep the username saved. So when a user comes back, it’s one less step for them to log in.
Zeldman.com has links in the footer which allow you to toggle the colors used on the site between two versions. It saves this as a cookie which is great, but is subject to the frailty of cookies. Any site like this with alternate styling choices (e.g. font-size toggle) could save that information in localStorage and have it persist better.
This is the tip of the iceberg. I’m sure people with more developer-oriented minds can think of bigger and better ideas for this. Saving your place in a game? Caching assets? What else you got?