HTML5 and REST

BTI360 Blog / January 19, 2011 in 

BTI’s web services team has been focusing on RESTful web clients in recent blog posts and videos, and one aspect of RESTful clients we’d like to explore next is HTML5. HTML5 is both an umbrella term for upcoming and ongoing enhancements to HTML, CSS, and JavaScript, and it also refers to the next major version number of the HTML standard. The increased attention paid to the subject in various websites, blogs, books, and other media sparked the following question: how can HTML5 affect the development of RESTful web applications, if at all? As the reader will see, there are certain aspects of HTML5 that can in fact affect the design and development of RESTful web services.

Browser Support

Current web browsers support HTML5 to varying degrees. For example, at html5test.com — a website that detects browser support for various HTML5-umbrella technologies– the author’s current version of Chrome 9.x scores 242 out of a possible 300 points; Firefox 4 beta 9 scores 207, Opera 11.0 scores 177, and IE 8 scores 27 points. When developing RESTful web applications, one way to make browser detection of HTML5 features easier is to use Modernizr, a small JavaScript library built for this purpose.  Once its script has been included in a web client, it can often be used like so, in JavaScript code:

if (Modernizr.some_property) {
	...
}

where the if statement returns true if Modernizr detects that the some_property feature is available in the browser.  For example, the statement

if (Modernizr.audio && Modernizr.audio.m4a)

first detects whether or not the browser supports the canPlayType property on an audio element, and then detects whether or not the browser supports playing of .m4a files. Other techniques for detecting HTML5 features involve checking properties on global objects or creating dummy elements and inspecting various properties or methods on these objects.  In the examples that follow, Modernizr or other detection techniques will not be discussed, but the author recommends their use for as long as HTML5 support in all browsers remains less than strong.

Geolocation

One new feature of HTML5 that can improve the user experience of RESTful location-based web applications is the new geolocation property of the global navigator object. This property allows the web browser to determine the values of certain geolocation-related properties, given that the browser conforms to the standard and that the underlying hardware / network / device on which the browser is installed can provide these properties. For example, a mobile browser can implement the geolocation API on a device that supports triangulation with a certain degree of accuracy, or a higher degree of accuracy on a GPS-enabled mobile device.

A simple example of use of this API is shown below:

function show_location() {
	navigator.geolocation.getCurrentPosition(show_coords);
}
function show_coords(position) {
	var lat = position.coords.latitude;
	var lon = position.coords.longitude;
	alert(“latitude is “ + lat + “ and longitude is “ + lon);
}

where a call to show_location would cause the alert to appear. Along with latitude and longitude, the coords object also supports altitude, accuracy, altitudeAccuracy, heading, and speed properties, and a timestamp property is available on the position object. The geolocation fields can make the process of interacting with a RESTful geolocation API easier and more accurate for the user, as the browser can determine geo-properties that would otherwise have to be externally determined and manually entered by the user into the web client.

Microdata

In the 2007 book RESTful Web Services, the authors devote a chapter to “The Building Blocks of Services”, which includes a discussion of represent

Previous

Building a RESTful Web Service with the Play Framework

Next

RESTful Client with the Play Framework

Close Form

Enjoy our Blog?

Then stay up-to-date with our latest posts delivered right to your inbox.

  • This field is for validation purposes and should be left unchanged.

Or catch us on social media

Stay in Touch

Whether we’re honing our craft, hanging out with our team, or volunteering in the community, we invite you to keep tabs on us.

  • This field is for validation purposes and should be left unchanged.