In this article, I intend to provide a similarly comprehensive overview of HTML5 with an emphasis on features oriented toward mobile development. We'll dive into some specific examples of HTML5 features and focus specifically on what is available with mobile devices. I will focus on what developers can do today as opposed to what is part of the specific set of standards. I'll also mention where a product may have a slightly different outcome than expected.
HTML5 introduces many new tags and attributes. In this section, we'll take a look at the ones you as a developer will likely need to use immediately.
HTML5 page. First off, an HTML5 page is much easier to set up than a web page in earlier HTML versions. Previous versions of HTML and XHTML had very complicated XML namespace support that average developers were confused by and honestly didn't pay much attention to. HTML5 has a much simpler page definition and will be simpler for developers to get started with.
Let's look at an HTML5 template, shown in Figure 1, which I created for my HTML5 samples (available for download with the online article; see the top of page for the download button). There are a few items to note in the sample code:
- The <DOCTYPE/> tag is much simpler than we formerly saw in HTML. The tag instructs the browser to run in standards-compliant mode.
- The <meta/> tag with the http-equiv attribute instructs the browser to use the most recent version of the HTML engine in the browser. In addition, if the user has the Google Chrome Frame plug-in installed, the Chrome Frame engine is used in Internet Explorer (IE). (See the sidebar "Author's Notes" at the end of this article for more information about Chrome Frame.)
- The <script/> tag does not have the type or language attribute. These attributes are not required for HTML5. There is a new attribute for the <script/> tag named async. The async attribute allows for the script to be executed asynchronously, or not.
- The jquery.tmpl.min.js and Google maps files are used for their templating capabilities and are not a requirement for HTML5.
- Within the <body/> tag, there are additional content sections. These include the <header/>, <section/>, <article/>, <footer/>, and other tags. These tags allow for a page to be divided into logical sections that can provide additional information to a page.
Multimedia tags. The most discussed, and controversial, feature in HTML5 is its support for video, which is provided by the <video/> and <audio/> tags. Before these tags existed, developers had to opt for building Rich Internet Application (RIA) type of solutions that may have included Silverlight and Flash to get support for audio and video.
Let's look at the <audio/> tag first. The <audio/> tag offers developers a standardized way to provide audio to web applications. The following example shows how to use the <audio/> tag:
<source src="@Href("~")Content/HTML5.mp3" type="audio/mpeg" />
As you can see, the <source/> tag indicates that HTML5 supports the .mp3 file type. Various file types can be specified in the <source/> tag (e.g., .ogg).
HTML5's <video/> tag provides video support. There is a major difference between audio and video in the browser world. With audio, MP3 has widespread support among the various browsers that developers will encounter. There's no equivalent "default" video format in the marketplace. Figure 2 shows a short example that uses the <video/> tag.
Just as the <audio/> tag does for audio, the <video/> tag provides the basic infrastructure for supporting video. The biggest problem with the <video/> tag is the lack of agreement by major vendors on which formats they support. I won't bore you with the details of this ongoing disagreement. But as you can see in the example in Figure 2, you can specify a number of different formats via the <source/> tag.
The biggest problem for developers will be creating the video files in the necessary formats for display to users. And in addition to creating and specifying the file formats, developers will need to be careful with the MIME types that are loaded and set up on the server. The installation of Microsoft IIS 7.5 that I used needed to be configured to send these files to the user, so you will most likely need to set up these features. In my example, I set up the necessary MIME types by adding the entries shown in Figure 3 to the web.config file. I've found two tools very helpful in creating the video in the necessary format: Firefogg and HandBrake.
Input tags and attributes. We've had the same set of input tags in HTML for many years. These include <input/>, <select/>, <form/>, and others. These tags have served us well. We've learned over the past few years that there are numerous user input scenarios that we can optimize. For example, how many times have you used a text field to allow the user to input a phone number? You may have had to use some logic to limit the input to numbers or another similar form. It would be nice if we had a tag that was optimized for a phone number. HTML5 provides this along with other new input types. The following examples demonstrate how to use HTML5 input tags:
<input id="email" name="email" type="email" placeholder="Email" autocapitalize="off" />
For a desktop web browser, this may not be a big deal, but for a mobile user, the tel and email input types can be a godsend. The tel type is used as a hint to the browser to open a keyboard that is optimized for phone input, like the one shown in Figure 4. This keyboard will handle primarily numeric input. The email type will result in a keyboard being opened that is optimized for inputting an email address.
I'm sure that developers are now thinking, "This is great, but what happens when I run this on IE8 or another browser that doesn't support these new input types?" Thankfully, when a browser, such as an old version of IE or Firefox, encounters an input type that it does not understand, the browser will display the input tag as a text field. This allows a developer to start using these new input types and still have backward compatibility with existing browsers. Note that I've covered only a few of the input types. HTML5 provides more input types to handle dates, numbers, and other types of input data.
In addition to new input types, numerous new attributes are available. Two new attributes worth mentioning here are placeholder and autocomplete attributes. The placeholder attribute allows for text to be placed within a TextField class as a watermark, as shown in Figure 5.
The content in the watermark is not stored as input, it is merely displayed. This capability can be used to display helpful information to the user without taking up valuable screen real estate. For example, the downloadable code examples I've provided include a user registration sample, in which a test is performed on the page-ready event. In this test, if the browser supports the placeholder attribute, the textual explanations for the input fields are turned off, and instead a placeholder is used to provide the user with information regarding the TextField while conserving on-screen space, which is at a premium in a mobile device. The autocomplete attribute can be used to turn off the browser's auto-completion help.
Two additional attributes, though not part of the official HTML5 specification, are useful in the mobile world with the iPhone and Android. The first, the autocorrect attribute, can be used to turn on/off auto-correction. This can be helpful depending on how users feel about auto-correction. The second, the autocapitalize attribute, can be handy when inputting proper names. The following example shows how to use these attributes.
autocorrect="off" autocapitalize="on" autocomplete="off" autofocus="true" /></span>
There are more new tags and attributes in the HTML5 specification. You can find a larger listing of the tags here.
Geolocation. Although geolocation support is not an official part of the HTML5 specification, it's hard to overlook it. For the purposes of this article, we'll consider it a part of HTML5. With geolocation, it's possible for a browser to request the location information from the underlying operating system. This occurs by performing the following steps:
- A browser check is performed to verify that the browser supports geolocation. In the example in Figure 6, the check is performed using the Modernizr library.
- The getCurrentPosition method is called with the three parameters. The first parameter is required, and the second two are optional.
a. The first parameter is a callback on success. This callback will be called when the geolocation value is determined.
b. The second parameter is a callback on error. This callback method happens when there is an error determining the user location.
c. The third parameter is options that can be passed in. In this example, enableHighAccuracy is turned on.
- The web browser will ask the user if they want to provide the web app access to the user's location information.
- In this example, a map point of the user's location is displayed on a map.
- In this example, the watchPosition method is used to display location updates.
- Though not shown, the timeout and maximumAge options can be used. The timeout option is the maximum time allowed for obtaining the location. The maximumAge option is used to determine how many milliseconds to cache the location.
Figure 7 displays the results of the code in Figure 6, with a map showing the user's location.
One problem that should be mentioned is that the watchPosition method seems to have a bleed-over of the various types of geolocation functions in the device. As a result, it is important to pay attention to the accuracy of values that are returned. In my own experience, I have found that the iPhone is more accurate than Android devices; however, this may be due to my specific location and not the device OS itself. You can find more information about the Geolocation API here.
Web SQL. Storing data is a problematic issue with web applications. Web developers are familiar with cookies, a type of persistent data storage. Over time, the need for data storage support for web apps has grown. To solve some of these problems, the localStorage and sessionStorage attributes have been added and accepted into web browsers. Unfortunately, these attributes don't provide the more complex types of storage support that developers need. To address this need, the Web SQL standard was created. Web SQL allows for more data to be stored in a relational format that many developers are familiar with.
Unfortunately, Web SQL has run afoul of the standards process. Although the Web SQL standard exists, it is not being enhanced further. For the desktop, this is an issue; however, Web SQL has been supported in Android and iPhone, so it has tremendous support in the mobile environment.
Here are the steps that the code performs:
- The openDatabase command is used to create a database if it doesn't already exist. Otherwise the database is opened.
- Various SQL commands are passed in as necessary. These commands include commands to drop a table, create a table, and fill that table using a parameterized SQL command. In this example, the database commands are wrapped in a transaction.
- An SQL statement is used to query the database table.
- The data is output into an HTML table.
Although the Web SQL standard exists, further work on it has been shelved pending changes in the marketplace and standards process. What are developers to do regarding local data in a web application? Work is currently going on for a standard called IndexedDB. Hopefully, this standard will gain support in the mobile marketplace in the near future. You can find more information about IndexedDB here.
Other HTML5 Features
Although I've covered a number of HTML5 features in this article, it is by no means a complete list. Here are some additional features in HTML5:
- Drag and drop -- With drag and drop, users can grab objects and drag them around the screen to operate on them.
- Cross-document messaging -- For a number of years, web developers have known that it is impossible to make Ajax requests to domains that are not a part of the domain as well as script across windows loaded from different domains. This is a security feature. With support for cross-document messaging, pages can communicate with each other with basic security features enabled.
- Browser history support -- Support for the forward and backward buttons in a web application with Ajax is problematic. Various methods have been devised for this purpose; however, browser support has been spotty. With browser support for history built in, developers will be better off. The window.history object supports several methods worth mentioning:
- pushState(state, title, url) allows for the pushing of state into the browser's history stack.
- window.onpopstate is an event that will be raised when a user navigates through a page supporting history.
- history.replace, .back, .forward, and .go are methods that will allow users to navigate through the history as needed by the application.
- Microdata -- With microdata, it will be possible to add additional meaning to web applications in the browser. Microdata will give more meaning to the content so that search engines can provide better search results.
- File access -- There are several APIs that provide access to the file system on the client computing device.
Along with these features, there is CSS3, which space limitations prevent me from discussing here. You can find more information about HTML5 features as well as CSS3 in "HTML5 Is in Style: Working with CSS3 and HTML5."
One final note: As you develop applications that use HTML5, you may also want to make use of frameworks. The examples here are based on the jQuery Mobile framework, which we will explore further in "Get Started Using jQuery Mobile for Mobile Web Development." I have used it to due to the popularity of jQuery among ASP.NET developers. However, jQuery Mobile is by no means the only framework that you can use. Some of the others are jQTouch, Sencha touch, iWebKit, Wink, and others. I hope that you have enjoyed this introduction to HTML5 and will use this article as a jumping-off point in your explorations of these and other HTML5-related technologies and tools.
Here are a few considerations to be aware of as you're reading this article:
- As a developer, I have many "interesting" things installed on my system. One of those things is the Google Chrome Frame, which is a Google-written plug-in for Internet Explorer (IE). Google Chrome Frame is not the Google Chrome browser for IE. Rather, it is a plug-in that brings some HTML5 features to IE 6, 7, and 8. This was on my system before I installed IE 9 -- thus, you may experience some differences in output when you run the code samples.
- There are various third-party browsers for Android. Some support the HTML5 features mentioned in the article, some do not. For example, Firefox on Android does not support Web SQL, whereas the built-in Android browser does. Because of these variations, I highly recommend using the Modernizr framework to test a browser for feature support.
- Because of the number of browsers and combinations, it's important to use feature detection in an application as opposed to browser detection.
- The HTML5 samples written with this article use Razor and ASP.NET Web Pages. The key pieces are in the individual pages. (For more information on these topics, see Brian Mains' article "Using ASP.NET Web Pages with the Razor View Engine."
- The jQuery Mobile libraries used are based are the daily build of jQuery Mobile. This can result in breakage one day and everything working properly on another day.