Book Image

HTML5 Enterprise Application Development

By : Nehal Shah, Gabriel José Balda Ortíz
Book Image

HTML5 Enterprise Application Development

By: Nehal Shah, Gabriel José Balda Ortíz

Overview of this book

<p>HTML5 has been a trending topic for a long time, but the lack of Flash support on iOS devices has sped up its penetration. New features in HTML5 come at a time when web developers are pushing the limits of what is achievable and HTML5, CSS3, and JavaScript have become an important alternative for building rich user interfaces.<br /><br />"HTML5 Enterprise Application Development" will guide you through the process of building an enterprise application with HTML5, CSS3, and JavaScript through creating a movie finder application. You will learn how to apply HTML5 capabilities in real development problems and how to support consistent user experiences across multiple browsers and operating systems, including mobile platforms.<br /><br />This book will teach you how to build an enterprise application from scratch using HTML5, CSS3, JavaScript, and external APIs.<br /><br />You will discover how to develop engaging experiences using HTML5 capabilities, including video and audio management, location services, and 3D and 2D animations. We will also cover debugging techniques, automated testing, and performance evaluations to give you all the tools needed for an efficient development workflow.<br /><br />"HTML5 Enterprise Application Development" is a comprehensive guide for anyone who wants to build an enterprise web application. You will learn through the implementation of a real-world application as we show you handy libraries, development tips, and development tools.</p>
Table of Contents (20 chapters)
HTML5 Enterprise Application Development
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

HTML5, apart from being the latest buzzword in the Internet era, is quickly becoming the lingua franca for the web. In fact, HTML5 is the first version of HTML to get its own logo (http://www.w3.org/html/logo). To understand the significance of that, one must first know a little history.

A brief history of time (client-server edition)

Enterprise application development over the decades has been a pendulum swinging back and forth between terminal and mainframe, between client and server. In the 1980s, business logic was largely pushed to the server by "dumb terminals" or "thin clients" which did very little except act as a middleman between the user and the server. Beginning in the 1990s, logic started to swing to the client with "fat clients" bearing the processing burden. With the introduction of the World Wide Web in 1991, a new breed of thin client emerged. The pendulum swung once again. Or did it?

The shift between client and server has largely been driven by cost and power. Early on, investment was made in powerful, costly servers. As PCs became more powerful in terms of memory and processing ability, as well as lower in cost, it became possible to build applications that could be distributed more easily, allow for offline capabilities, and best of all require less powerful (and less costly) server infrastructures. However, maintaining, upgrading, and deploying "fat clients" created a new burden.

Web-based applications eventually emerged to solve the problems of cost, power, and maintainability. At first, they appeared to be much like their "thin client" predecessors: merely middlemen between users and servers but without the deployment overhead. However, with the introduction of technologies such as Java and JavaScript, the "thin client" began to put on a little weight. Before long, the processing burden began to bleed to the client as applets, scripts, and plugins became more commonplace and with them the maintainability problem reappeared. Rather than managing distributions of applications, the problem shifted to managing distributions of applets, scripts, and plugins.

The situation was bifurcated by the introduction of "rich clients". Business logic became tiered. A separation of concerns became the norm. Let the server deal with server stuff. Let the client deal with client stuff. The problem with this, however, is that the client took some time to be able to handle the client stuff the world needed of it.

A brief history of time (web browser edition)

When Tim Berners-Lee introduced his World Wide Web browser to his CERN colleagues in 1990, only a glimmer could be seen of what it would one day become. Marc Andreessen would introduce Mosaic and graphical browsing in 1993 and Netscape would soon follow in 1994. After that, Microsoft jumped in with Internet Explorer in 1995. Pretty soon, the first browser wars would come and go with Internet Explorer emerging as the victor and the remnants of Netscape coalescing around Mozilla and Firefox, which was released in 2002. In the 2000s, Apple released Safari and Google released Chrome.

Web browsers however have seldom held parity with one another. The existence of web standards and a governing body called the W3C notwithstanding, browser makers have nevertheless played to their own tunes. In the beginning, implementations of HTML varied widely and the trend continued through HTML 4.0, XHTML, CSS, and JavaScript. The variance in implementations and behavior made web development difficult and time consuming.

To combat the limitations and inconsistencies of the web browser, technologies based on plugins such as Adobe Flash began to flourish continuing where browser-based technologies such as HTML, CSS, and JavaScript left off. For years, many websites consisted mostly—and sometimes entirely—of technologies such as Flash; such plugins even afforded better performance. The notion of Rich Internet Applications (RIAs) prevailed as the pendulum swung back to fatter clients.

Note

See Google's "The Evolution of the Web" for an interactive graphic on web browsers and their implementation of modern browser features at http://evolutionofweb.appspot.com.

The inclusion of plugin-based technologies became a red herring for the promise of the World Wide Web. While connectivity of content was a hallmark of the original principles of HTML, content was represented by tags such as embed, object, and applet where application modules are embedded onto a web page constituted black boxes that hid content from the semantic web.

Web browsers nevertheless evolved. JavaScript frameworks such as jQuery emerged to abstract browser differences and offer up richer interactivity. CSS techniques emerged to overcome the limitations and inconsistencies between the browsers. Newer browsers emerged with better support for web standards.

However, something was missing. Even though applications were being developed using browser-based technologies, many application features were left out of the browser. Consistent mechanisms to add video/audio playback, offline capabilities, and even browser history management were missing. Flash was still looked upon as filling in the missing pieces of the web.

Finally, browser development coalesced around HTML5 in 2009 when XHTML 2.0 was abandoned in lieu of something more backward compatible with earlier versions of HTML. HTML5 sought to address two major areas in the browser: the need for a more consistent, semantic markup language and the demand for more natively-supported browser features. When it was introduced in 2004, it heralded a set of APIs to make the browser into a true application development platform and thus more semantically amenable.

Note

Features of HTML5

  • Media API: This embeds, plays, and pauses multimedia content

  • Text Track API: This reads properties of the text tracks of multimedia content

  • Drag and drop API: This natively makes elements draggable by setting an attribute

  • Offline Application Cache: This saves data locally for offline use

  • History API: This asserts more control of the back button

  • Canvas API: This literally draws all over the web in 2D and 3D

  • Cross Document Messaging: This overcomes cross-site scripting limitations

  • Microdata: This adds more semantic content for search engines to find

  • MIME Type and Protocol Handler Registration: This extends applications with handlers for additional protocols

  • Web Workers: This spawns threads independent of user interaction

  • Web Storage: This stores data on the client

  • Web Sockets: This sends two-way messages between server and client

With modern browsers' increasing support for HTML5, reliance on plugin-based technologies is starting to give way to browser-based implementations. With the APIs that allow for better control of the experience, the client is finally beginning to catch up to its promise. Mobile web browsers have especially become a catalyst for this. Since Adobe Flash is not supported on devices such as the iPhone and iPad, and since implementation of HTML5 features on Safari have grown, HTML5 is quickly becoming a standard for mobile web application development. However, if this trend is to continue, browser makers and application developers must adhere to the developing standards for HTML5, which brings us back to the logo. To right the wrongs of the past, HTML5 must have collective agreement on implementation. In order to inculcate this, there is a burgeoning movement to enforce standards in web browsers and applications, and speed up implementation as adoption looms. The HTML5 logo is emblematic of that effort.

Note

The Web Hypertext Application Technology Working Group (WHATWG) formed in 2004, evolved HTML and conceived of HTML5 as the next step in the evolution of the HTML standard. At that time, the W3C was working on the XHTML 2.0 standard; however, in 2009, the W3C decided to halt this effort and join the WHATWG in its effort to develop HTML5.

In January 2011, it announced that the HTML5 standard would be referred to as "HTML" and that the specification would henceforth be a living document.

In December 2012, the World Wide Web Consortium (W3C), the international web standards body, announced that HTML5 is feature complete. Although not a standard yet, it finally gives browser makers a stable target upon which to develop HTML5 features.

It's all about semantics

HTML5 makes an attempt to codify information on the web in a more cohesive way than before. With previous versions of HTML, content is structured based on how it should be displayed rather than its inherent meaning. The div tag is often used, but what does a div tag really mean? To get around this, application developers have broken up their content using id attributes based on standards and best practices of web presentation.

For example, application developers use tags such as the following:

<div id="header">
<div id="footer">

The obvious problem is that what gets used for the id attribute need not follow a standard. One application developer could use id="header" while another uses id="head". In order to standardize structure based on semantics, HTML5 introduces a set of new tags that takes the vagaries out of the process.

HTML5 introduces a set of new top-level tags, which can be broken down into the following categories: content, language, layout, and format.

Content tags

The content tags introduced in HTML5 define how new types of content can be embedded into a web page. Content such as sound, video, and graphics are surfaced in much the same way text and images have been for so many years.

  • audio: This tag is used for embedding sound content. Before HTML5, either some browsers implemented support for audio inconsistently or a special player typically developed using Adobe Flash would have been required to play sound. HTML5 removes that dependency and makes the audio player a consistent function of the web browser itself.

  • video: This tag is used for embedding video content. Like with audio, there was inconsistent support or a special player was required to play video content. Web browsers that support the video tag have a built-in video player.

  • canvas: This tag allows for basic 2D to be drawn via JavaScript. 3D graphics are not consistently supported, however, and are optional.

Language tags

With internationalization taking on more and more precedence, localization has been a special challenge for web developers. HTML5 introduces a set of new tags to help make content more accessible to larger audiences.

  • bdi: This tag defines the directionality of text. This is typically used to support languages that are read right-to-left.

  • ruby: The ruby tag in conjunction with the rt and rp tags defines the Ruby annotation for East Asian typography.

Layout tags

HTML5 comes with a set of first-class tags that not only help with laying out the page, but also allows for the page to be broken up altogether. With HTML5, web developers have the ability to share sections of content in a more standard way:

  • header: This tag defines the header of the page or of a section or article.

  • footer: This tag defines the footer of the page or of a section or article.

  • nav: This tag defines the menu structure of the website. These are the navigational links used to semantically break up the website.

  • section: This tag defines sections of a page. The article and aside tags are in a way specialized section tags.

  • aside: This tag defines the sidebar content for a page. Often, a web page is broken up with ancillary content pushed to the side.

  • article: This tag defines the main content for a page. While tags such as section, aside, header, and footer define ancillary content to the page, the article tag identifies the portion of content that is considered to be the focal point. Typically, this content is unique to the URI.

Format tags

HTML5 introduces a new set of special tags, which define how areas of content can be identified and formatted appropriately.

  • figure: This tag identifies non-contiguous content that is layered into a body of text. For example, it can be used to wrap diagrams, charts, and graphs that are referenced by a body of text.

  • details: This tag defines content that can be toggled as visible or hidden. It is geared towards showing and hiding content based on a user action such as help-related content. Web developers have built a variety of solutions to do this and, with HTML5, the web browser takes care of it.

  • hgroup: This tag wraps the well-known h1-h6 tags into a cohesive structure. When headings are related, hgroup shows that they are related. For example, for an article with a title and subtitle, the title would be wrapped in an h1 tag while the subtitle would be wrapped in an h2 tag. The hgroup tag around them signifies that the h2 tag is associated with the h1 tag and not part of the document outline.

  • wbr: This tag defines a word break opportunity. Typically, lines of text are broken up by spaces. The wbr tag allows for the web developer to specify where in a set of contiguous non-space characters line breaks can be introduced when there is no room to display it all on one line.

  • progress: This tag indicates the progress of a task and can be used in conjunction with JavaScript to display a progress bar to the user.

  • time: This tag is a microformat tag that allows one to specify semantically that something is a date or time.

  • meter: This tag is a format tag to define a scalar measurement with a known range.

  • mark: This tag indicates text that is relevant to the user. Typically, this would be used for highlighting specific words within a passage.

Forms get an upgrade

Forms in HTML5 get a whole new set of functionality to allow for better validation of content and ease of use.

The following tags are new with HTML5:

  • datalist: This tag works similarly to a select tag with the added feature of being able to type ahead to select items in the list.

  • keygen: This tag generates a key pair for use in forms. This is typically used for client authentication.

  • output: This tag indicates the result of a calculation. It is associated with a form tag to display simple calculations to the user especially when used in conjunction with the new form input types. In addition, the input tag gets a new set of types. The following input types are new with HTML5:

    • color: This type displays a color picker, which submits a hex code for that color.

    • date: This type displays a date picker, which submits a date.

    • datetime: This type displays a date and time picker, which submits a date and time including time zone.

    • datetime-local: This type displays a date and time picker, which submits a date and time without time zone.

    • email: This type displays a field for entering e-mail addresses.

    • month: This type displays a month-year picker, which submits a month and year.

    • number: This type displays a field constrained for entering numeric values.

    • range: This type constrains the user to select numbers within a range. Typically, this will display as a slider.

    • search: This type displays a search field.

    • tel: This type displays a field that constrains the user to typing in a valid telephone number.

    • time: This type displays a time picker.

    • url: This type displays a field that constrains the user to typing in a valid URL.

    • week: This type displays a control for picking a week within a year.

Enter microdata

HTML5 adds the ability to define custom semantics for your content. Similar to microformats in previous versions of HTML, in which a set of predetermined attributes would allow you to ascribe the semantic meaning of content, microdata allows you to create your own semantic language to ascribe to your content. While microformats rely on generic attributes such as class and rel, microdata introduces itemscope, itemtype, and itemprop to describe content. The itemscope and itemtype attributes allow you to define a custom type and indicate where it is defined. The itemprop attribute points to a specific property within the definition:

<div itemscope itemtype="http://mysite.com/Movie">
    <h1 itemprop="title">Your Favorite Movie</h1>
    <p itemprop="summary" >
      A summary of your favorite movie.
    </p>
</div>

An anatomy lesson

Now that we know many of the new tools for building a page in HTML5, let us dive into what a page looks like.

A word about DOCTYPE

The DOCTYPE declaration in HTML documents has always been a signal to the browser of the standards to which the document adheres. If a web browser knows the standards used for the document, it can more efficiently process and render that document. SGML-based markup languages require this.

In order to simplify the DOCTYPE tag, HTML5 has only one type:

<!DOCTYPE html>

Unlike previous versions of HTML, which required references to the specific DTD being followed, HTML5 is not based on SGML and thus the DTD reference is not required.

The lang attribute

HTML5 introduces a simplified lang attribute for specifying the language of a page. In XHTML, an xmlns attribute was required, but HTML5 does not require this.

Metatags are important too

HTML5 adds a new metatag called charset. This specifies the specific character encoding of the document. It otherwise uses all the metatags from HTML 4.01.

HTML5 includes support for the viewport metatag. This metatag defines how the web page should be viewed and includes parameters such as width and height. It also allows you to define zoom settings such as initial scale, and minimum and maximum scale. It even allows for the ability to target a specific density DPI in case you want to change how your page looks based on different screen resolutions.

Putting it all together

A basic HTML5 page will look like the following code:

<!doctype html>
<html lang="en">
<head>
  <title>My First HTML5 Page</title>
  <meta charset="utf-8">
  <meta name="description" content="My first HTML5 page.">
  <meta name="author" content="Me">
</head>
<body>
</body>
</html>

We will of course add more to this as we go on.

The application

For much of this book, we will be building a mobile web application that illustrates many of the features of HTML5. The application is called MovieNow, and will be a one-stop shop for finding, reviewing, and booking movies near you. The features we will develop in this book are as follows:

  • Find movies near you using geolocation

  • Display movie data to the user

  • View trailers using the video tag

  • Display reviews using the canvas tag

  • Select movies using the drag and drop API

  • Integration with external APIs

  • Display tweets near you via Web Workers

What this book covers

In the following chapters, we will build a variety of features of HTML5 into our MoveNow enterprise application.

Chapter 1, HTML5 Starter Kit: Compatibility, discusses support of HTML5 features across multiple web browsers and devices as well as ways to skirt the deficiencies of these browsers. The main driver for supporting multiple browsers is ensuring access to enterprise web applications across multiple devices while maintaining a consistent user experience.

Chapter 2, HTML5 Starter Kit: Useful Tools, provides a guide to getting started with HTML5 enterprise application development including available tools, their installation, and their use. A comprehensive evaluation of the business drivers for selecting tools will be discussed.

Chapter 3, The App: Structure and Semantics, walks you through setting up the boilerplate for the MovieNow enterprise application. We will set up the overall page structure, discuss semantic tags in depth, and talk about microdata.

Chapter 4, The App: Getting Movies via Geolocation, begins the MovieNow enterprise application by introducing geolocation. We will walk you through the geolocation API and how to use it to implement useful features.

Chapter 5, The App: Displaying Movie Data via CSS3, covers layout and features of CSS3 including some interesting CSS3 effects. We will also cover best practices in defining standard styles across web applications and devices including media queries and compatibility considerations for CSS3.

Chapter 6, The App: Trailers via HTML5 Video, introduces the video and audio tags and their use within an HTML5 enterprise application. We will talk about manipulating the playback of multimedia content via JavaScript as well as backward compatibility with browsers that do not support the HTML5 video and audio tags.

Chapter 7, The App: Showing Ratings via Canvas, walks through HTML5 canvas and using the drawing API to display graphics in your enterprise applications. We will use the drawing API to create ratings charts for our MovieNow application.

Chapter 8, The App: Selection UI via Drag-and-Drop, covers the drag-and-drop API. We will implement the drag-and-drop functionality in the MovieNow enterprise application demonstrating event delegation and the publish-subscribe pattern.

Chapter 9, The App: Getting the Word Out via Twitter, discusses forms and form validation in HTML5 by integrating with the Twitter API. We will implement posting of tweets from within the MovieNow application.

Chapter 10, The App: Consuming Tweets via Web Workers, demonstrates Web Workers and the power of external APIs to bring social elements to enterprise application. We will integrate tweets near you into the MovieNow application.

Chapter 11, Finishing Up: Debugging Your App, talks about ways of debugging HTML5 enterprise applications. We will discuss the browser console and HTTP proxies.

Chapter 12, Finishing Up: Testing Your App, covers tools for testing HTML5 enterprise applications. We will cover functional test suites and automated tools.

Chapter 13, Finishing Up: Performance, discusses performance, which is a crucial topic for any HTML5 enterprise application. We will talk about strategies and tools and walk through profiling your HTML5 application.

What you need for this book

You will need prior knowledge of web application development as this book introduces developers already familiar with the basics of the web including HTML, CSS, and JavsScript to the advantages of HTML5 and CSS3.

Who this book is for

This book is primarily aimed at application developers who have some experience developing applications for the web, and want to extend their knowledge to the latest developments in HTML5 and CSS3. Upon completion of this book, readers will have a thorough understanding of the toolset that HTML5 provides to develop enterprise applications.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "Copy html5shiv.js from the dist folder to your JavaScript folder".

A block of code is set as follows:

<div class="geolocation-available">
  Congrats! Your browser supports Geolocation!
</div>
<div class="no-geolocation">
  Your browser doesn't support Geolocation :(
</div>

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Click on the GENERATE! button".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or e-mail .

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Downloading the color images of this book

We also provide you a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from http://www.packtpub.com/sites/default/files/downloads/5689_graphics.pdf.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.