So far, I’ve dabble with pieces of HTML5, having build the rite-of-passage Todo application - now it’s time to jump in and build a more complete application. In a series of posts, I’m going to build an HTML5 application from scratch and document my experiences along the way. Let’s start off by exploring HTML5’s Web Storage feature.
The application I’ve decided to build is a timer application - basically, you enter a time interval (in hours, minutes, seconds), and it’ll notify you when that time has been reached. I start things off by creating the basics of what the application would look like via HTML:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Couple of notes about the above snippet:
The doctype and lack of attribute quoting are standard to the spec. One of the key points of HTML5 was identify the lowest common denominator between the popular browsers and based the spec around them. In this case, the doctype of html was the minimum needed to be a valid doctype and attribute values do not need to be quoted unless there are whitespace in the value.
Line #21: I’m using the new placeholder attribute which puts a placeholder text in the text input when the input is empty and doesn’t have value. Once you put focus on that text input and enter a value, the place holder text goes away.
Next comes the thymer.js script. Nothing HTML5 specific here, I’m just putting the code here for completeness.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
I now have a basic timer application. Now for the HTML5 goodness.
For any desktop to be truly useful (desktop or web), it needs to be able to remember state from the last time you ran the application. For desktop applications, there are many different ways to do this, but web applications have always been limited to cookie or server-based storage. In HTML5, there are actually a few options for storing data, with the most commonly supported one being the Local Storage API. Local Storage API is a simple API that allows you to store data as key/value pairs. For my Thymer application, I’m going to store the timer array so any created timers will be persisted across browser reloads.
First, I update the Timer object so it can be converted to/from a save state:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Then I save the timer array every time a new timer is created, and load the stored timers when the application starts:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
Some notes about the Local Storage API:
localStorage only stores string values, so use JSON.stringify() and JSON.parse() to convert your data to/from JSON.
In addition to setItem and getItem, there are also clear and removeItem functions available.
You can also access stored values directly as properties on the localStorage object (e.g. window.localStorage.timers).
Values are stored to an origin, which is the combination of scheme (http/https) + host + port. This means other pages on that server could access those stored values, so attention should be paid to avoid naming collisions.
Origin pinning also means that if a page can be viewed via multiple domains (www.example.com and example.com for example), you won’t be able to access stored values between those two domains. Consider using permanent redirects to get users under one domain.
Local Storage doesn’t seem to work in Firefox with pages served via the file:// protocol - you’ll need to serve your page from a web server when testing locally.
So that’s it for the part 1. A lot of this is common knowledge for those who have already read up on HTML5, but now that we’ve setup the basic of the application, we can move on to more interesting aspects of HTML5. In part 2, I’ll cover playing audio natively and using Chrome’s notification API.
You can get the final code for Part I here. If you’re interested in going deeper into HTML5, I highly recommend visiting Mark Pilgrim’s Dive Into HTML5 and getting his HTML5: Up and Running book.