Working with HTML5 localStorage()
HTML5 is fast approaching and getting wide spread usage. Most are familiar with the new tags like header, section and footer and everybody is aware of the new video tags thanks to Apple vs Adobe. However, there is a lot more to HTML5 those just those two aspects.
I want to focus on localStorage() this time. localStorage is a client-side key-value database, meaning it is stored in the users browser. This means the users data is saved on their machine inside their browser. This also means that their data is only available to them when on that machine and in that browser. Remember that localStorage is per browser not per computer.
Browser support is always a huge topic when discussing new html5 or even css3 aspects. This is due to such a wide spread of support from the big browsers right now. In the case of localStorage, it is supported by Safari 4+, Mobile Safari (iPhone/iPad), Firefox 3.5+, Internet Explorer 8+ and Chrome 4+. This only leaves out Opera who currently does not support this storage. Depending on your target audience browser support may be an issue for you.
HTML5 localStorage() is a four part series of articles where I will show you how to create a simple html5 time tracking web app using localStorage as the sole database. The series will walk you through the basics of localStorage to advanced techniques to leverage the power of this new html5 database. We won’t stop at just the functionality of the app but in part four we will cover styling the app with html5 and css3 to create an iPhone(ish) web app.
Here is a quick example of what the javascript looks like to access localStorage();
if (typeof(localStorage) == ‘undefined’ ) {
alert(‘Your browser does not support HTML5 localStorage. Try upgrading.’);
} else {
try {
localStorage.setItem(“name”, “Hello World!”); //saves to the database, “key”, “value”
} catch (e) {
if (e == QUOTA_EXCEEDED_ERR) {
alert(‘Quota exceeded!’); //data wasn’t successfully saved due to quota exceed so throw an error
}
}
document.write(localStorage.getItem(“name”)); //Hello World!
localStorage.removeItem(“name”); //deletes the matching item from the database
}
In the code snippet above we are doing several things including checking if your browser supports localStorage, saving a new item to the database, retrieving that item and displaying it and then removing the item. The above is your typical Hello World example but shows you just how easy it is to use. The time tracking app we will create will be storing a unique id as the key and joining multiple values into one string, as localStorage only supports storing of strings. I will be making the source code available at the end of each article along with the full source and the working app at the end of the series, which you can follow here.
” Written for HTML5Tutorial.net by James Fleeting, a web designer and developer obsessed with the future of the web working at Crane | West Advertising Agency in Wichita Falls, Texas. “
4 Responses
5.12.2010
Nice article, I was reading up on HTML5 & came across localStorage on these sites too if its any help to someone:
http://googlegeodevelopers.blogspot.com/2010/05/google-apis-html5-new-era-of-mobile.html
http://dev.w3.org/html5/webstorage/#the-localstorage-attribute
5.13.2010
Hi James,
Nice article – I wanted to ask you if you think localStorage() will replaces Cookies for storing user data.
Thanks,
Mark
5.13.2010
Thanks for the comments.
Mark: I do believe localStorage() stands a chance to replace cookies for some uses. At the moment there is a difference in that you can have a cookie expire itself and with something stored in localStorage you can’t. I like that, for at least the moment, a user would need to take steps to delete their local database for a site. Cookies are a little easier, or at the very least more common in people deleting.
Some people don’t think twice about deleting their cookies but if your storing more data in localStorage you don’t want that user to just dump the database and not think twice.
7.18.2010
Thanks! What about security though? JavaScript is always accessible to the user so they can view source and see the localStorage structure and key/value pairs. I’m not a JavaScript hater or anything, it’s just that this is one of the many bonuses to server-side scripting. You can hide stuff you don’t want the user to necessarily see.