- •Contents at a Glance
- •Contents
- •Foreword
- •About the Authors
- •About the Technical Reviewer
- •Acknowledgments
- •Introduction
- •Who This Book Is For
- •An Overview of This Book
- •Example Code and Companion Web Site
- •Contacting the Authors
- •Overview of HTML5
- •The Story So Far—The History of HTML5
- •The Myth of 2022 and Why It Doesn’t Matter
- •Who Is Developing HTML5?
- •A New Vision
- •Compatibility and Paving the Cow Paths
- •Utility and the Priority of Constituencies
- •Interoperability Simplification
- •Universal Access
- •A Plugin–Free Paradigm
- •What’s In and What’s Out?
- •What’s New in HTML5?
- •New DOCTYPE and Character Set
- •New and Deprecated Elements
- •Semantic Markup
- •Simplifying Selection Using the Selectors API
- •JavaScript Logging and Debugging
- •window.JSON
- •DOM Level 3
- •Monkeys, Squirrelfish, and Other Speedy Oddities
- •Summary
- •Using the Canvas API
- •Overview of HTML5 Canvas
- •History
- •What Is a Canvas?
- •Canvas Coordinates
- •When Not to Use Canvas
- •Fallback Content
- •CSS and Canvas
- •Browser Support for HTML5 Canvas
- •Using the HTML5 Canvas APIs
- •Checking for Browser Support
- •Adding a Canvas to a Page
- •Applying Transformations to Drawings
- •Working with Paths
- •Working with Stroke Styles
- •Working with Fill Styles
- •Filling Rectangular Content
- •Drawing Curves
- •Inserting Images into a Canvas
- •Using Gradients
- •Using Background Patterns
- •Scaling Canvas Objects
- •Using Canvas Transforms
- •Using Canvas Text
- •Applying Shadows
- •Working with Pixel Data
- •Implementing Canvas Security
- •Building an Application with HTML5 Canvas
- •Practical Extra: Full Page Glass Pane
- •Practical Extra: Timing Your Canvas Animation
- •Summary
- •Working with Scalable Vector Graphics
- •Overview of SVG
- •History
- •Understanding SVG
- •Scalable Graphics
- •Creating 2D Graphics with SVG
- •Adding SVG to a Page
- •Simple Shapes
- •Transforming SVG Elements
- •Reusing Content
- •Patterns and Gradients
- •SVG Paths
- •Using SVG Text
- •Putting the Scene Together
- •Building an Interactive Application with SVG
- •Adding Trees
- •Adding the updateTrees Function
- •Adding the removeTree Function
- •Adding the CSS Styles
- •The Final Code
- •Summary
- •Working with Audio and Video
- •Overview of Audio and Video
- •Video Containers
- •Audio and Video Codecs
- •Audio and Video Restrictions
- •Browser Support for Audio and Video
- •Using the Audio and Video API
- •Checking for Browser Support
- •Accessibility
- •Understanding Media Elements
- •Working with Audio
- •Working with Video
- •Practical Extras
- •Summary
- •Using the Geolocation API
- •About Location Information
- •Latitude and Longitude Coordinates
- •Where Does Location Information Come From?
- •IP Address Geolocation Data
- •GPS Geolocation Data
- •Wi-Fi Geolocation Data
- •Cell Phone Geolocation Data
- •User–Defined Geolocation Data
- •Browser Support for Geolocation
- •Privacy
- •Triggering the Privacy Protection Mechanism
- •Dealing with Location Information
- •Using the Geolocation API
- •Checking for Browser Support
- •Position Requests
- •Building an Application with Geolocation
- •Writing the HTML Display
- •Processing the Geolocation Data
- •The Final Code
- •Practical Extras
- •What’s My Status?
- •Show Me on a Google Map
- •Summary
- •Using the Communication APIs
- •Cross Document Messaging
- •Understanding Origin Security
- •Browser Support for Cross Document Messaging
- •Using the postMessage API
- •Building an Application Using the postMessage API
- •XMLHttpRequest Level 2
- •Cross-Origin XMLHttpRequest
- •Progress Events
- •Browser Support for HTML5 XMLHttpRequest Level 2
- •Using the XMLHttpRequest API
- •Building an Application Using XMLHttpRequest
- •Practical Extras
- •Structured Data
- •Framebusting
- •Summary
- •Using the WebSocket API
- •Overview of WebSocket
- •Real-Time and HTTP
- •Understanding WebSocket
- •Writing a Simple Echo WebSocket Server
- •Using the WebSocket API
- •Checking for Browser Support
- •Basic API Usage
- •Building a WebSocket Application
- •Coding the HTML File
- •Adding the WebSocket Code
- •Adding the Geolocation Code
- •Putting It All Together
- •The Final Code
- •Summary
- •Using the Forms API
- •Overview of HTML5 Forms
- •HTML Forms Versus XForms
- •Functional Forms
- •Browser Support for HTML5 Forms
- •An Input Catalog
- •Using the HTML5 Forms APIs
- •New Form Attributes and Functions
- •Checking Forms with Validation
- •Validation Feedback
- •Building an Application with HTML5 Forms
- •Practical Extras
- •Summary
- •Working with Drag-and-Drop
- •Web Drag-and-Drop: The Story So Far
- •Overview of HTML5 Drag-and-Drop
- •The Big Picture
- •Events to Remember
- •Drag Participation
- •Transfer and Control
- •Building an Application with Drag-and-Drop
- •Getting Into the dropzone
- •Handling Drag-and-Drop for Files
- •Practical Extras
- •Customizing the Drag Display
- •Summary
- •Using the Web Workers API
- •Browser Support for Web Workers
- •Using the Web Workers API
- •Checking for Browser Support
- •Creating Web Workers
- •Loading and Executing Additional JavaScript
- •Communicating with Web Workers
- •Coding the Main Page
- •Handling Errors
- •Stopping Web Workers
- •Using Web Workers within Web Workers
- •Using Timers
- •Example Code
- •Building an Application with Web Workers
- •Coding the blur.js Helper Script
- •Coding the blur.html Application Page
- •Coding the blurWorker.js Web Worker Script
- •Communicating with the Web Workers
- •The Application in Action
- •Example Code
- •Summary
- •Using the Storage APIs
- •Overview of Web Storage
- •Browser Support for Web Storage
- •Using the Web Storage API
- •Checking for Browser Support
- •Setting and Retrieving Values
- •Plugging Data Leaks
- •Local Versus Session Storage
- •Other Web Storage API Attributes and Functions
- •Communicating Web Storage Updates
- •Exploring Web Storage
- •Building an Application with Web Storage
- •The Future of Browser Database Storage
- •The Web SQL Database
- •The Indexed Database API
- •Practical Extras
- •JSON Object Storage
- •A Window into Sharing
- •Summary
- •Overview of HTML5 Offline Web Applications
- •Browser Support for HTML5 Offline Web Applications
- •Using the HTML5 Application Cache API
- •Checking for Browser Support
- •Creating a Simple Offline Application
- •Going Offline
- •Manifest Files
- •The ApplicationCache API
- •Application Cache in Action
- •Building an Application with HTML5 Offline Web Applications
- •Creating a Manifest File for the Application Resources
- •Creating the HTML Structure and CSS for the UI
- •Creating the Offline JavaScript
- •Check for ApplicationCache Support
- •Adding the Update Button Handler
- •Add Geolocation Tracking Code
- •Adding Storage Code
- •Adding Offline Event Handling
- •Summary
- •The Future of HTML5
- •Browser Support for HTML5
- •HTML Evolves
- •WebGL
- •Devices
- •Audio Data API
- •Touchscreen Device Events
- •Peer-to-Peer Networking
- •Ultimate Direction
- •Summary
- •Index
Introduction
HTML5 is brand new. Indeed, it isn’t even completely finished yet. And if you listen to some ornery pundits, they’ll tell you that HTML5 won’t be ready for ten years or more!
Why, then, would anyone think that now’s the time for a book called Pro HTML5 Programming? That’s easy. Because for anyone who’s looking for an extra edge to make your web application stand above the rest, the time for HTML5 is right now. The authors of this book have been working with, developing, and teaching HTML5 technologies for more than two years now and can claim with certainty that adoption of the new standards is accelerating at dizzying speeds. Even over the course of writing this book, we’ve been forced to continually update chapters and reevaluate our assumptions about what is ready to use.
Most users don’t really understand the power that’s available in the browsers they are now using. Yes, they might notice some minor interface enhancement after their favorite browser has automatically updated. But they might have no idea that this new browser version just introduced a free-form drawing canvas or real-time network communication, or any number of other potential upgrades.
With this book, we aim to help you unlock the power HTML5.
Who This Book Is For
The content in this book is intended for the experienced web application developer who is familiar with JavaScript programming. In other words, we won’t be covering the basics of web development in this text. There are many existing resources to get you up to speed in the fundamentals of web programming. That said, if you see yourself in any of the following bullets, this book will likely provide you with useful insight and information you are looking for:
•You sometimes find yourself thinking, “If only my browser could. . .”
•You find yourself using page source and developer tools to dissect a particularly impressive website.
•You enjoy reading the release notes of the latest browser updates to find out what’s new.
•You are looking for ways to optimize or streamline your applications.
•You are willing to tailor your website to provide the best possible experience for users on relatively recent browsers.
If any of these apply to you, this book may be a good fit with your interests.
While we take care to point out the limitations of browser support where appropriate, our aim is not to give you elaborate workarounds to make your HTML5 application run seamlessly on a ten-year-old browser. Experience has shown that the workarounds and baseline browser support are evolving so rapidly that a book like this is not the best vehicle for such information. Instead, we will focus on the
xix
INTRODUCTION
specification of HTML5 and how to use it. Detailed workarounds can be found on the Internet and will become less necessary over time.
An Overview of This Book
The thirteen chapters of this book cover a selection of popular, useful, and powerful HTML5 APIs. In some cases, we have built on the capabilities introduced in earlier chapters to provide you with richer demonstrations.
Chapter 1, “Introduction to HTML5,” starts off with the background of past and current versions of the HTML specification. The new high-level semantic tags are presented, along with basic changes and the rationale behind all the recent developments in HTML5. It’s good to know this terrain.
Chapter 2, “Using the Canvas API”, Chapter 3, “Working with SVG,” and Chapter 4, “Working with Audio and Video,” describe the new visual and media elements. In these chapters, the focus is on finding simpler ways to spruce up your user interface without plugins or server-side interaction.
Chapter 5, “Using the Geolocation API,” introduces a truly new feature that was not easily emulated before now—the ability for an application to identify the user’s current location and use that to customize the experience. Privacy is important here, so we cover some of the caveats, as well.
The next two chapters, “Using the Communication APIs” and “Using the WebSocket API,” present increasingly powerful ways that HTML5 lets you communicate with other websites and stream real-time data to an application with both simplicity and minimal overhead. The techniques in these chapters will enable you to simplify the many overly complex architectures deployed on the Web today.
Chapter 8, “Using the Forms API,” presents you with minimal tweaks you can make to your desktop or mobile web applications today to increase usability, as well as more fundamental changes you can make to detect page entry errors in very common usage scenarios. Chapter 9, “Using the Drag and Drop API,” elaborates on the new Drag and Drop API features and shows how you can use them.
Chapters 10, 11, and 12—“Using the Web Workers API,” “Using the Storage API,” and “Creating Offline Web Applications”—deal with the internal plumbing of your applications. Here, you will find ways to optimize the existing functionality to obtain better performance and better data management.
Finally, Chapter 13, “The Future of HTML5,” will give you a tasty preview of what’s still to come.
Example Code and Companion Web Site
The code for the examples shown in this book is available online in the Source Code section of the Apress web site. Visit www.apress.com, click Source Code, and look for this book’s title. You can download the source code from this book’s home page. In addition, we are hosting a companion site for the book at www.prohtml5.com, from which you can also download the sample code and some practical extras.
Contacting the Authors
Thank you for buying this book. We hope you enjoy reading it and that you find it a valuable resource. Despite our best effort to avoid errors, we realize that things sometimes slip through the cracks, and we’d like to express, in advance, our regrets for any such slip-ups. We welcome your personal feedback, questions, and comments regarding this book’s content and source code. You can contact us by sending us e-mail at prohtml5@gmail.com.
xx