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. “
14 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.
12.20.2010
How long will the storage been kept? Can users remove the storage themself on client-side? Thanks.
1.16.2011
wingdragon, you are able to remove the storage using a web inspector such as the Safari’s one.
Best,
Guillermo.
2.8.2011
@wingdragon: Users are able to remove localStorage themselves. It can be done via the browsers Developer Tools as well as from clearing site data from the browser. Although most users wouldn’t delete the localStorage for a site they technically can if wanted to, much the same as Cookies.
2.13.2011
I just wondered, are there limitations on how much data can be saved under one name?
Thanks.
4.26.2011
TheDoctor — yes, 5MB/origin
5.6.2011
Can the database with localStorage() be saved to the Users computer/device (like in a cookie, txt file, etc.) and called at a different time during a different browser session?
Thanks for the Article!
7.2.2011
Hi
Can anyone give me an idea on how to use localstorage with asp.net
I want to save an asp.net variable to localstorage and retrieve it on another page in code behind. All examples shows on how to do this on an JavaScript onclick event.
I need to do this in a .net function.
Any help will be highly appreciated.
Thanks
7.29.2011
@Bennie, where as cookies are passed in the http headers of each request/response the localstorage never leaves the local machine and as such server side technology can not touch it.
you would need to set up some kind of client server model, ideally Ajax/Ajaj that allows the javascript running on the client to talk to the server
11.20.2011
@Dennis:
I am not exactly sure because I am fairly new to JavaScript but I believe if you write your JavaScript in what is know as a “Revealing Module Pattern” you can have public variables and methods and private variables and methods. Anything that is public is what someone will be able to see using any tool. The private methods and variables are hidden and not accessible by the end user and all thats available to them is the public info. I hope this helps you out. The sources for articles is limited but if you Google “Revealing Module Pattern” it might shed some more light. I am currently using this pattern for college right now.
1.17.2012
Hello ,
i am using local storage for saving and retrieving .i want to get all data which i am saving though my app in one go.i.e i need a single retrieve function to retrieve all my saved data …How can i achieve this ..