2. The Disclaimer I’m not an expert… So… Feel free to share your expertise! Most of this presentation is a synopsis of the excellent book Dive into HTML5 by Mark Pilgrim. An online version of the text can be found at http://diveintohtml5.org/. Legal and everything!
3. A bit of motivation “HTML was primarily designed as a language for semantically describing scientific documents, although its general design and adaptations over the years have enabled it to be used to describe a number of other types of documents. The main area that has not been adequately addressed by HTML is a vague subject referred to as Web Applications. This specification attempts to rectify this, while at the same time updating the HTML specifications to address issues raised in the past few years.” from the HTML5 Working Draft (19 October 2010)
4. Can I use it today? Detect browser support using Javascript. Modernizr can help you out for this. The Modernizr homepage will show a feature grid for the current browser. Check out your favorite browser at http://www.modernizr.com/.
5. So can I use it today… on the desktop? Your mileage may vary… This should improve with the release of IE9.
6. So can I use it today… on mobiles? Looking good!
7. The topics we’ll cover More and less markup Drawing things Showing videos Geolocation Offline storage The offline application cache Microdata
8. The topics we won’t cover CSS3 New form elements Web Sockets Web Workers Web SQL Database And many more!
9. Learn by example We’ll dissect an example as we go along. It will showcase every featured topic. It’s a poor man’s mobile application. Leverage superior support on mobile platforms. (I’m hoping to become a mobile developer someday.)
11. More and less markup (I) A new and simple doctype: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN“ "http://www.w3.org/TR/xhtml1/DTD/xhtml1- strict.dtd"> <!DOCTYPE html>
12. More and less markup (II) A new and simple root element: <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <html lang="en">
13. More and less markup (III) New and simple character encoding selection: <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta charset="utf-8" />
14. More and less markup (IV) Slightly more simple stylesheets: <link rel="stylesheet" href="style-original.css" type="text/css" /> <link rel="stylesheet" href="style-original.css" />
15. More and less markup (V) New link types: nofollow, prefetch, ...
16. More and less markup (VI) New semantic elements: <section>, <nav>, <article>, <aside>, <hgroup>, <header>, <footer>, <time>, <mark>, ... Be wary: Weirdness may happen when the browser doesn't (yet) recognize these elements!
17. Drawing things (I) “The canvas element provides scripts with a resolution-dependent bitmap canvas, which can be used for rendering graphs, game graphics, or other visual images on the fly.” from the HTML5 Working Draft (19 October 2010)
18. Drawing things (II) Use the <canvas> element to insert a canvas: <canvas id="myCanvas" width="300" height="225"/>
19. Drawing things (III) Get access to the drawing context via Javascript: var myCanvas = document.getElementById("myCanvas"); var myContext = myCanvas.getContext("2d");
22. Drawing things (VI) Draw some text: myContext.font = "bold 12px sans-serif"; myContext.fillText("<canvas> is sweet!", 40, 50);
23. Drawing things (VII) Handle some click events: function handleClick(clickEvent) { var x, y; if ( clickEvent.pageX != undefined && clickEvent.pageY != undefined ) { x = clickEvent.pageX; y = clickEvent.pageY; } else { x = clickEvent.clientX + document.body.scrollLeft + document.documentElement.scrollLeft; y = clickEvent.clientY + document.body.scrollTop + document.documentElement.scrollTop; } x -= myCanvas.offsetLeft; y -= myCanvas.offsetTop; // You should probably do something useful here. } myCanvas.addEventListener("click", handleClick, false);
24. Drawing things (VIII) Internet Explorer 8 doesn't support canvas. It does support drawing via the Vector Markup Language (VML). The explorercanvas library implements canvas using VML. It does have some limitations. Find it at http://code.google.com/p/explorercanvas/
25. Showing videos (I) “A video element is used for playing videos or movies.” from the HTML5 Working Draft (19 October 2010)
26. Showing videos (II) Inserting a video should be as easy as inserting an image: Browsers should have built-in support for playing video. No third-party plugins should be required. Standard formats should exist that are supported by all browsers. But...
27. Showing videos (III) Surprizingly, no standard formats exist that are supported by all browsers. For maximum compatibility you will have to supply content in multiple formats. HTML 5 will let you specify multiple files in different formats and have the browser select one it supports.
28. Showing videos (IV) Use the <video> element to insert a video: <video src="movies/alikeforlife.mp4" width="480" height="272" controls />
30. Showing videos (VI) Compliant browsers ignore non-<source> children of <video> elements. A nested <object>tag can be included for backwards compatibility. Programmatic control is possible via Javascript.
31. Geolocation (I) “The Geolocation API defines a high-level interface to location information associated only with the device hosting the implementation, such as latitude and longitude.” from the Geolocation API Candidate Recommendation (7 September 2010)
32. Geolocation (II) Request the current position: function callback(position) { var timestamp = position.timestamp; var latitude = position.coords.latitude; var longitude = position.coords.longitude; // ... } navigator.geolocation.getCurrentPosition(callback);
33. Geolocation (III) The position is returned asynchronously. Location information can come from a number of sources. A GPS unit is not necessarily required. “User agents must not send location information to Web sites without the express permission of the user.” Usually a popup will explicitly ask for permission.
34. Geolocation (IV) Do some error handling: function errorHandler(error) { var code = error.code; var message = error.message; // ... } navigator.geolocation.getCurrentPosition(callback, errorHandler);
35. Geolocation (V) Set some options: var options = { enableHighAccuracy: true, timeout: 10000, maximumAge: 60000 } navigator.geolocation.getCurrentPosition(callback, errorHandler, options);
36. Geolocation (VI) Do some continuous tracking: var ticket = navigator.geolocation.watchPosition( callback, errorHandler, options); navigator.geolocation.clearWatch(ticket);
37. Geolocation (VII) You may want to support additional geolocation frameworks: Gears Blackberry Nokia ... The geo-location-javascript library provides a unified geolocation interface. Find it at http://code.google.com/p/geo-location-javascript/.
38. Offline storage (I) “This specification defines an API for persistent data storage of key-value pair data in Web clients.” from the Web Storage Working Draft (22 December 2009)
39. Offline storage (II) Load and store data via the localStorage object: localStorage.setItem("someProperty", "someValue"); var value = localStorage.getItem("someProperty");
40. Offline storage (III) You may prefer associative arrays: localStorage["someProperty"] = "someValue"; var value = localStorage["someProperty"];
41. Offline storage (IV) Store information as key/value pairs. Values are stored as strings. You'll have to coerce the value to the proper type if it's not a string. Everything happens client-side. Unlike cookies, no data is sent to the server. It's even supported in Internet Explorer 8!
42. Offline storage (V) The specification suggests an arbitrary limit of 5 megabytes per origin. Storage is per domain. Cross-directory attacks are possible on shared hosts!
43. The offline application cache (I) “In order to enable users to continue interacting with Web applications and documents even when their network connection is unavailable (...) authors can provide a manifest which lists the files that are needed for the Web application to work offline and which causes the user's browser to keep a copy of the files for use offline.” from the HTML5 Working Draft (19 October 2010)
44. The offline application cache (II) Enable offline use of your web-application: All application resources are downloaded into an offline cache when the user visits your webpage. The page can then be served from the cache, even when the user is offline.
45. The offline application cache (III) A manifest lists the resources of your application: CACHE MANIFEST # Revision 99 images/logo.jpg index.php stylesheet.css
46. The offline application cache (IV) You reference the manifest from every HTML file: <html manifest="/cache.manifest"> <!–– ... ––> </html>
47. The offline application cache (V) The manifest is checked on every page visit. When the manifest has changed, the resources are recached. This process is automatic. Javascript events allow you to know what's going on.
48. The offline application cache (VI) You can have three sections in your manifest: A cache section. Required resources to be stored in the cache. A network section. Uncached resources that will only be referenced when online. A fallback section. Resources matching these URL patterns will be satisfied via the network when online, or will be mapped on the specified resource when offline.
49. The offline application cache (VII) The manifest file must be served with the text/cache-manifestcontent-type. Only resources listed in the manifest can be accessed, even when online. The offline cache will only be updated when the manifest file itself changes. Adding a revision count to your manifest is a good idea. When a new version of your page is cached it will take an additional reload of the page to switch to the new cache.
50. Microdata (I) “This mechanism allows machine-readable data to be embedded in HTML documents in an easy-to-write manner, with an unambiguous parsing model.” from the HTML Microdata Working Draft (19 October 2010)
51. Microdata (II) Add custom-domain semantic information to your HTML markup. Make it so that a machine can interpret the information on your webpage within the specified domain.
52. Microdata (III) The semantic domain of HTML is constrained to documents. It lets you describe such things as sections, headers, paragraphs, etc. Via microdata you can add semantic meaning to a specific tag. The content of a specific <span> tag might for instance be the name of a person.
53. Microdata (IV) A vocabulary defines: A specific semantic object, f.i. a Person. A number of properties that this semantic object can have, f.i. a name or an email address. A vocabulary is identified by means of a URI.
54. Microdata (V) How it might look in your HTML code: <section itemscope itemtype="http://data-vocabulary.org/Person"> <h1>Contact Information</h1> <dl> <dt>Name</dt> <dd itemprop="name">John Doe</dd> </dl> </section>
55. Microdata (VI) Google's spider interprets microdata. Annotated pages will appear semantically formatted in search results.