HTML5 Introduction


Published on

An introduction to HTML5 by Olivier Verhoogen, mobile developer at Foreach.

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

HTML5 Introduction

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