exam-ref-70-480-programming-in-html5-with-javascript-and-css3.pdf

July 25, 2017 | Author: Alberto MN | Category: Html, Document Object Model, Web Page, Html5, Search Engine Indexing
Share Embed Donate


Short Description

Download exam-ref-70-480-programming-in-html5-with-javascript-and-css3.pdf...

Description

Exam Ref 70-480 Programming in HTML5 with JavaScript and CSS3

Rick Delorme

PUBLISHED BY Microsoft Press A Division of Microsoft Corporation One Microsoft Way Redmond, Washington 98052-6399 Copyright © 2014 by Rick Delorme All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher. Library of Congress Control Number: 2014940678 ISBN: 978-0-7356-7663-3 Printed and bound in the United States of America. First Printing Microsoft Press books are available through booksellers and distributors worldwide. If you need support related to this book, email Microsoft Press Book Support at [email protected] Please tell us what you think of this book at http://www.microsoft.com/learning/booksurvey. Microsoft and the trademarks listed at http://www.microsoft.com/en-us/legal/intellectualproperty/Trademarks/ EN-US.aspx are trademarks of the Microsoft group of companies. All other marks are property of their respective owners. The example companies, organizations, products, domain names, email addresses, logos, people, places, and events depicted herein are fictitious. No association with any real company, organization, product, domain name, email address, logo, person, place, or event is intended or should be inferred. This book expresses the author’s views and opinions. The information contained in this book is provided without any express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its resellers, or distributors will be held liable for any damages caused or alleged to be caused either directly or indirectly by this book. Acquisitions Editor: Anne Hamilton Developmental Editor: Karen Szall Editorial Production: Box Twelve Communications Technical Reviewer: Todd Meister Cover: Twist Creative • Seattle

Contents Introduction

ix

Microsoft certifications

ix

Free ebooks from Microsoft Press

x

Errata, updates, & book support

x

We want to hear from you

x

Stay in touch

x

Preparing for the exam

xi

Chapter 1 Implement and manipulate document structures and objects

1

Objective 1.1: Create the document structure. . . . . . . . . . . . . . . . . . . . . . . . . 2 Using HTML5 semantic markup

2

Creating a layout container in HTML

15

Optimizing for search engines

16

Optimizing for screen readers

17

Objective summary

20

Objective review

21

Objective 1.2: Write code that interacts with UI controls . . . . . . . . . . . . . . 22 Adding or modifying HTML elements

22

Implementing media controls

34

Implementing graphics with HTML5 and SVG

39

Objective summary

58

Objective review

59

What do you think of this book? We want to hear from you! Microsoft is interested in hearing your feedback so we can continually improve our books and learning resources for you. To participate in a brief online survey, please visit:

www.microsoft.com/learning/booksurvey/ iii

Objective 1.3: Apply styling to HTML elements programmatically. . . . . . 60 Changing the location of an element

61

Applying a transform

63

Showing and hiding elements

67

Objective summary

71

Objective review

71

Objective 1.4: Implement HTML5 APIs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Using the storage API

72

Using the AppCache API

77

Using the Geolocation API

81

Objective summary

85

Objective review

85

Objective 1.5: Establish the scope of objects and variables . . . . . . . . . . . . 86 Establishing the lifetime of variables and variable scope

87

Avoiding using the global namespace

90

Leveraging the this keyword

91

Objective summary

92

Objective review

92

Objective 1.6: Create and implement objects and methods. . . . . . . . . . . . 93 Implementing native objects

94

Creating custom objects

95

Implementing inheritance

99

Objective summary

101

Objective review

101

Answers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102

Chapter 2 Implement program flow

111

Objective 2.1: Implement program flow. . . . . . . . . . . . . . . . . . . . . . . . . . . . 112

iv

Contents

Evaluating expressions

112

Working with arrays

117

Implementing special types of arrays

121

Using advanced array methods

122

Implementing iterative control flow

125

Objective summary

129

Objective review

130

Objective 2.2: Raise and handle an event. . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Using events

131

Handling DOM events

139

Creating custom events

146

Objective summary

147

Objective review

148

Objective 2.3: Implement exception handling. . . . . . . . . . . . . . . . . . . . . . . 149 Implementing try…catch…finally constructs

149

Checking for null values

154

Objective summary

155

Objective review

155

Objective 2.4: Implement a callback. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Implementing bidirectional communication with the WebSocket API

157

Making webpages dynamic with jQuery and AJAX

161

Wiring up an event with jQuery

165

Implementing a callback with an anonymous function

167

Using the this pointer

169

Objective summary

170

Objective review

171

Objective 2.5: Create a web worker process. . . . . . . . . . . . . . . . . . . . . . . . . 172 Getting started with a web worker process

172

Creating a worker process with the Web Worker API

176

Using web workers

178

Understanding web worker limitations

179

Configuring timeouts and intervals

180

Objective summary

181

Objective review

181

Answers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183

Contents

v

Chapter 3 Access and secure data

189

Objective 3.1: Validate user input by using HTML5 elements. . . . . . . . . . 190 Choosing input controls

190

Implementing content attributes

206

Objective summary

209

Objective review

210

Objective 3.2: Validate user input by using JavaScript. . . . . . . . . . . . . . . . 211 Evaluating regular expressions

211

Evaluating regular expressions in JavaScript

214

Validating data with built-in functions

216

Preventing code injection

216

Objective summary

217

Objective review

218

Objective 3.3: Consume data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 Consuming JSON and XML data by using web services

219

Using the XMLHttpRequest object

219

Objective summary

223

Objective review

223

Objective 3.4: Serialize, deserialize, and transmit data. . . . . . . . . . . . . . . . 224 Sending data by using XMLHttpRequest

224

Serializing and deserializing JSON data

225

Serializing and deserializing binary data

225

Objective summary

228

Objective review

229

Answers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230

Chapter 4 Use CSS3 in applications

235

Objective 4.1: Style HTML text properties. . . . . . . . . . . . . . . . . . . . . . . . . . 235

vi

Contents

Apply styles to text appearance

236

Apply styles to text font

238

Applying styles to text alignment, spacing, and indentation

239

Applying styles to text hyphenation

241

Applying styles for a text drop shadow

242

Objective summary

243

Objective review

243

Objective 4.2: Style HTML box properties. . . . . . . . . . . . . . . . . . . . . . . . . . 244 Applying styles to alter appearance attributes

244

Applying styles to alter graphic effects

249

Apply styles to establish and change an element’s position

258

Objective summary

264

Objective review

265

Objective 4.3: Create a flexible content layout. . . . . . . . . . . . . . . . . . . . . . 266 Implement a layout using a flexible box model

266

Implementing a layout using multi-column

273

Implementing a layout using position, floating, and exclusions 276 Implementing a layout using grid alignment

280

Implementing a layout using regions, grouping, and nesting

286

Objective summary

287

Objective review

288

Objective 4.4: Create an animated and adaptive UI. . . . . . . . . . . . . . . . . . 288 Animating objects by applying CSS transitions

289

Applying 3-D and 2-D transformations

291

Adjusting UI based on media queries

292

Hiding or disabling controls

297

Objective summary

299

Objective review

299

Objective 4.5: Find elements using CSS selectors and jQuery . . . . . . . . . 300 Defining element, style, and attribute selectors

300

Choosing the correct selector to reference an element

301

Finding elements by using pseudo-elements and pseudo-classes

301

Objective summary

304

Objective review

305

Contents

vii

Objective 4.6: Structure a CSS file by using CSS selectors. . . . . . . . . . . . . 305 Referencing elements correctly

306

Implementing inheritance

307

Overriding inheritance using !important

307

Styling an element based on pseudo-elements and pseudo-classes 308 Objective summary

309

Objective review

309

Answers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312

Index

321

What do you think of this book? We want to hear from you! Microsoft is interested in hearing your feedback so we can continually improve our books and learning resources for you. To participate in a brief online survey, please visit:

www.microsoft.com/learning/booksurvey/ viii

Contents

Introduction The 70-480 exam is a detailed examination of your skills with using HTML5 and CSS3. This book will guide you through the necessary objectives that you are expected to know to pass this exam. It is expected that you have at least 2 years’ experience working with these technologies. This book is structured such that it provides a reference to the key information required for each objective. This book does not teach every concept but provides an account of the details you are expected to know for each objective covered on the exam. This book covers every exam objective, but it does not cover every exam question. Only the Microsoft exam team has access to the exam questions themselves and Microsoft regularly adds new questions to the exam, making it impossible to cover specific questions. You should consider this book a supplement to your relevant real-world experience and other study materials. If you encounter a topic in this book that you do not feel completely comfortable with, use the links you’ll find in text to find more information and take the time to research and study the topic. Great information is available on MSDN, TechNet, and in blogs and forums.

Microsoft certifications Microsoft certifications distinguish you by proving your command of a broad set of skills and experience with current Microsoft products and technologies. The exams and corresponding certifications are developed to validate your mastery of critical competencies as you design and develop, or implement and support, solutions with Microsoft products and technologies both on-premise and in the cloud. Certification brings a variety of benefits to the individual and to employers and organizations. MORE INFO  ALL MICROSOFT CERTIFICATIONS

For information about Microsoft certifications, including a full list of available certifications, go to http://www.microsoft.com/learning/en/us/certification/cert-default.aspx.

ix

Free ebooks from Microsoft Press From technical overviews to in-depth information on special topics, the free ebooks from Microsoft Press cover a wide range of topics. These ebooks are available in PDF, EPUB, and Mobi for Kindle formats, ready for you to download at: http://aka.ms/mspressfree Check back often to see what is new!

Errata, updates, & book support We’ve made every effort to ensure the accuracy of this book and its companion content. You can access updates to this book—in the form of a list of submitted errata and their related corrections—at: http://aka.ms/ER480R2 If you discover an error that is not already listed, please submit it to us at the same page. If you need additional support, email Microsoft Press Book Support at [email protected] Please note that product support for Microsoft software and hardware is not offered through the previous addresses. For help with Microsoft software or hardware, go to http://support.microsoft.com.

We want to hear from you At Microsoft Press, your satisfaction is our top priority, and your feedback our most valuable asset. Please tell us what you think of this book at: http://aka.ms/tellpress The survey is short, and we read every one of your comments and ideas. Thanks in advance for your input!

Stay in touch Let’s keep the conversation going! We’re on Twitter: http://twitter.com/MicrosoftPress.

x Introduction

Preparing for the exam Microsoft certification exams are a great way to build your resume and let the world know about your level of expertise. Certification exams validate your on-the-job experience and product knowledge. While there is no substitution for on-the-job experience, preparation through study and hands-on practice can help you prepare for the exam. We recommend that you round out your exam preparation plan by using a combination of available study materials and courses. For example, you might use this Exam Ref and another study guide for your "at home” preparation and take a Microsoft Official Curriculum course for the classroom experience. Choose the combination that you think works best for you. Note that this Exam Ref is based on publicly available information about the exam and the author’s experience. To safeguard the integrity of the exam, authors do not have access to the live exam.

xi

This page intentionally left blank

CHAPTER 1

Implement and manipulate document structures and objects Web developers today need to understand the complexities of the constructs involved in building interactive and dynamic applications with HTML and JavaScript. The introduction of HTML5 brought a new important standard for defining the structure of your webpages as Have you read well as changes in how you interact with them via script. page xi? This chapter demonstrates how to create HTML5 documents with the new HTML5 semantic markup. You’ll explore the process of creating the code required to manipulate and interact with HTML5 markup and applying styles to HTML5 elements.

It contains valuable information regarding the skills you need to pass the exam.

NOTE  ELEMENTS VS. TAGS

HTML markup is referred to as both HTML tags and HTML elements. These terms are often used interchangeably. This book refers to the HTML markup as elements.

Objectives in this chapter: ■■

Objective 1.1: Create the document structure

■■

Objective 1.2: Write code that interacts with UI controls

■■

Objective 1.3: Apply styling to HTML elements programmatically

■■

Objective 1.4: Implement HTML5 APIs

■■

Objective 1.5: Establish the scope of objects and variables

■■

Objective 1.6: Create and implement objects and methods

1

Objective 1.1: Create the document structure Writing an HTML page can be very easy or very daunting, depending on your objectives. Any HTML page renders in a browser even if it contains only plain text. But this type of web application might not be effective at delivering your intended message or at providing interactivity to keep users coming back to the site for more. This is where HTML markup comes in handy. HTML enables you to apply a fluid and organized structure to webpages. Paired with a powerful tool such as JavaScript, HTML5 lets you deliver highly interactive content that can pique your users’ interest. This objective focuses on the semantic elements available to you in HTML5, which, along with JavaScript, allow you to create the rich end-user experience that modern web users want and have come to expect. The purpose of a document’s structure is to tell the browser how the content should be displayed. Without any declarative structure in your page, the browser won’t detect any structure, so it will lay out your content according to the rules implemented by its rendering engine. When using the HTML5 markup presented in this objective, you are telling the browser to take your semantics into account when displaying the page. Going forward, new releases of browsers will incorporate more and more of the HTML5 standards into their rendering engines. The exam will test your ability to use HTML5 semantic markup to create webpages and your ability to optimize webpages for use on screen readers. The exam will also cover the effect that the HTML5 semantic markup will have on search engine optimization.

This objective covers how to: ■■

Use HTML5 semantic markup

■■

Create a layout container in HTML

■■

Optimize for search engines

■■

Optimize for screen readers

Using HTML5 semantic markup Table 1-1 lists the HTML5 semantic elements as defined by the specification. These elements make up the core of HTML5. As such, understanding the definition and proper usage of each element is critical to successful completion of the exam. In the following sections, you will use each of these semantic markup elements to create a complete document structure.

2

CHAPTER 1

Implement and manipulate document structures and objects

TABLE 1-1  HTML5 semantic markup

HTML5 element

Description



Defines self-contained areas on a page



Defines smaller content areas outside the flow of a webpage



Defines the caption of a figure element



Defines content that contains a figure, such as an image, chart, or picture



Defines the bottom of a section or page



Defines the top of a section or page



Defines a group of headings (H1–H6 elements)



Defines text that should be highlighted



Defines navigation to other pages in the site



Defines the progress of the task



Defines the distinct content of a document

NOTE  USE ONLY WHAT YOU NEED

When designing a webpage, you don’t necessarily need to use every available element. Instead, use only the elements you need to get your job done.

Understanding the core structure of an HTML5 page Although a browser can render any plain text file, to provide any structure to the document the page must contain the basic elements that you are about to learn. Although this book assumes you have a basic understanding of how webpages are structured, the following HTML code demonstrates the basic template of an HTML5 page:



Objective 1.1: Create the document structure

CHAPTER 1

3

This section elaborates on this basic structure as semantic markup is introduced throughout the objective. All content to be introduced to the page will go within the element because that page section displays content in the browser. As it is now, this code would render nothing more than a blank page. To see some content as you work through the following sections for each semantic element, use the code in Listing 1-1. LISTING 1-1  HTML5 semantic elements Some fictional company Website Document Structure Writing Code Styles Our first new Article Section 1 Some details about section 1 Did you know that 7/10 is 70% Section 2 Second huge article Provide some useful information in the article Third huge article Provide some useful information in the third article Fig 1: A really juicy orange.

4

CHAPTER 1

Implement and manipulate document structures and objects

Using the and elements Most webpage documents contain common content at the top and bottom of all pages. Although using the and elements doesn’t automatically provide this functionality, the elements do provide the ability to define the content in the header and footer of the website. Typically, a webpage header contains content such as a company logo or banner. In some cases, it might also contain a navigation menu. (See the upcoming “Using the element” section for this.) Start the example page by adding the element to your page: Some fictional company Website …

This HTML code produces the output shown in Figure 1-1:

FIGURE 1-1 A element, which causes the browser to format the text within the element as a

heading.

The element isn’t limited to only the start of your page—it provides a semantic way of declaring the header to any area of the webpage. You can use the element as a header to a element or to an element. The element is intended to hold an H1–H6 element as needed; however, you can populate a header with any markup that suits your needs to create the best header for that particular area of the site. This HTML code produces the output shown in Figure 1-2: … Some fictional company Website Our first new Article …

FIGURE 1-2 The element with a nested element



Objective 1.1: Create the document structure

CHAPTER 1

5

Using the element Using the element in an HTML5 document provides users with navigation through the main elements of the web document or web application as a whole. These main navigation elements could be represented as a list of links across the top of the page to navigate the current website. It could also list your favorite websites along the side of the page, such as in a blog where you list other favorite blogs that you follow. Typically, the list of links across the top, commonly known as the main menu of the web application, is contained in the header (but doesn’t have to be). A list of favorite URLs would most likely be placed into an so that the list could be placed off to the side, away from the main content but readily accessible. For the current example, you create a main menu across the top of the page. This HTML code produces the output in Figure 1-3: Some fictional company Website Document Structure Writing Code Styles …

FIGURE 1-3  A navigation menu created using the element

The other main element commonly used under the element is the rightfully named .

6

CHAPTER 1

Implement and manipulate document structures and objects

Using the element The element is a semantic method that organizes headers and subheaders. This element typically contains the standard and familiar to elements. The element groups related headers in sequence. You can add a new element to your webpage to serve this purpose, like so: … Our first new Article Provide some useful information in the article

This HTML code renders the output in Figure 1-4.

FIGURE 1-4  Using the element to group headers

This code example effectively tells the renderer that the article has a main heading (). You could go on, adding to elements if required. How many of these ( to ) elements you use in your element obviously depends on the document structure you want to present. Now that the page is starting to take shape, you can learn about the two main content elements: and .



Objective 1.1: Create the document structure

CHAPTER 1

7

Using the element An element represents a whole and complete composition or entry. Examples of an element could be a magazine article or a blog post, where the content can be redistributed independently and not lose its meaning. Each article is wholly contained within itself. You can have an article with subarticles; however, each subarticle must be a direct extension and related to the root article. Now that you know about this new element, you can go back to your sample document and organize it with articles: … Our first new Article Section 1 Some details about section 1 Did you know that 7/10 is 70% Section 2 Second huge article Provide some useful information in the article Third huge article Provide some useful information in the third article

This HTML code produces the output in Figure 1-5.

8

CHAPTER 1

Implement and manipulate document structures and objects

FIGURE 1-5  Using the element to segment different articles on the page

You have expanded the document to include three articles. Clearly, you don’t want to put the entire chapter into the sample HTML document, but you’ll add enough to be able to demonstrate the function of each semantic element. Each article added to the document in this example represents an independent part of the document that can be wholly contained. Typically, the first element within the element is a header element or header group. Closely related to the element is the element, which you explore next.

Using the element The element subdivides pages into sections. You could continue to break down the sample page with additional elements; however, the purpose of the element isn’t to break down a page into more granular details. This is where the element becomes useful. Each element contains zero or more elements to denote the different content sections within the element. Like an element, the first element within a element is typically a header or a header group.



Objective 1.1: Create the document structure

CHAPTER 1

9

Now you can expand the sample page with elements to produce the output shown in Figure 1-6: … Our first new Article Section 1 Some details about section 1 Section 2 …

FIGURE 1-6  Using the element within an element

When you view this page in the browser now, you might notice something quite interesting. The elements inside the elements are rendering differently from the previous elements in the same article. Considering that you haven’t applied any styles to this page yet, you might expect that all elements would render in the same style (for example, the same font size) as the element in the element. However, when you render the page in the browser, you can see that this isn’t the case. This is because of the way the document parser in the browser works through the document to determine the implied hierarchy of the headings, also called the document outline. You see more of this when you learn about screen readers later in this lesson. For now, focus on the element. 10

CHAPTER 1

Implement and manipulate document structures and objects

Using the element The element defines any content that doesn’t fall within the main flow or main content of the current page—for example, a sidebar, a note, an alert, or an advertisement. The element doesn’t place itself automatically to any particular side of the webpage; it merely serves as a way to semantically define a section of text or graphics as an aside. Later you will see how to position an aside using styles. For now, add the following element to your page for later use: … Our first new Article Section 1 Some details about section 1 Did you know that 7/10 is 70% Section 2 …

This HTML code produces the output in Figure 1-7:

FIGURE 1-7  Using an element within an element



Objective 1.1: Create the document structure

CHAPTER 1

11

As you can see in the browser output, the element isn’t treated as special in any way compared to the other elements used to structure your page. However, a little later you can see how much more easily you can style the content by using semantic markup, such as the element.

Using the and elements The and elements, new in HTML5, provide the semantic elements necessary for adding graphics and figures to webpages. These graphics and figures typically provide a visual representation of information in the textual content and referenced by the text. You often see such images in tutorials or textbooks in which the author directs the reader to a specific figure. As an example, I’ve added some HTML to the end of the previous example: … … Fig 1: A really juicy orange.

This HTML code produces the output in Figure 1-8.

FIGURE 1-8  Using the element to add a graphic or figure to the page

12

CHAPTER 1

Implement and manipulate document structures and objects

You need to replace the example image with one you already have to get the page to render correctly. However, the essence of what you achieve with the and elements should be clear.

Using the element The element represents the progress of an objective or task. The two supported types of progress tasks are determinate and indeterminate. Use a determinate progress task when you know in advance the amount of work to be completed; in other words, you know the starting and ending values. Sample scenarios for this case include downloading a file for which you know the exact size or displaying the progress of a fundraising effort. In both situations, you know the exact status of the task at any particular time, and you also know what the end goal is—either the number of bytes for the file download or the number of dollars for the fundraiser. In these determinate cases, you can specify HTML5 markup such as this: Our goal is to have 1000 users: 0 1000

The result of this HTML is the progress bar shown in Figure 1-9.

FIGURE 1-9  Using the element to show progress toward a total

As shown in the preceding code, the element has two attributes you need to know: value and max. The value attribute lets you specify the current value or position of the element at a specific point in time. The max attribute tells the browser what the maximum possible value is for the element. The browser uses these two values to determine how much of the element should be colored in. Usually, the value attribute updates dynamically using JavaScript. In Figure 1-10, you can see how the element’s display changes when the value attribute is updated to 750.

FIGURE 1-10  The effect of the value attribute on the element



Objective 1.1: Create the document structure

CHAPTER 1

13

You use indeterminate tasks when you don’t know how long a task will take to complete but still want to show users that some work is occurring and that they should wait. You still use the element but remove the value attribute. When you don’t specify the value attribute, the browser can infer that the element represents an indeterminate task. This might be useful for data received from a service where you have no control over or knowledge of how quickly the request will complete or how large the request results will be. The following HTML5 markup demonstrates an indeterminate task: Data download is in progress, please wait patiently:

This code produces a progress display like the one shown in Figure 1-11:

FIGURE 1-11  Showing indeterminate progress by using moving dots to demonstrate that work is occurring

In Figure 1-11, the blue dots replace the progress bar from the previous determinate example. This visual change to the progress indicator occurred by simply removing the value attribute from the element. In actuality, the dots are animated, as you would see if you run the code in a browser.

element With the element, you can easily highlight important information or any text you want to emphasize. It has essentially the same function as a highlighter. By wrapping text in a element and providing a background-color attribute to its style element, you can get the desired highlight effect. The following HTML code demonstrates the element: Some very important information provided here!

Figure 1-12 shows the output of this HTML. The word “important” is highlighted as a result of the element being placed around it.

FIGURE 1-12  The effect on text placed inside the element

14

CHAPTER 1

Implement and manipulate document structures and objects

Using the element The new HTML5 semantic elements don’t (with the exception of the element) necessarily provide any default or altered behavior. Instead, they provide a stronger semantic definition to your webpages. This, in turn, gives you a more reliable and maintainable way to structure your pages and style them consistently. The goal of these elements is to replace the older method of structuring pages—prior to HTML5—by using elements and naming them according to their function. However, note that the element is still part of HTML5 and still plays an important role. Use the new semantic elements as appropriate, but remember that the element is still quite useful for styling content. This section explored the new semantic elements in HTML5. In the next section, you learn how to create and work with layout containers.

Creating a layout container in HTML You can lay out a webpage in various ways. An important aim here is to urge you to give serious thought to layout so that your page presentation is user friendly. If users can’t find what they are looking for because the entire page is styled as a single element inside the element, they aren’t likely to keep coming back. In this section, you look at a couple of layout options available in HTML. Chapter 4, “Use CSS in applications,” explains how to use cascading style sheets (CSS) to implement your layouts. The two most common methods of creating a layout in HTML involve using and elements. In either case, more than likely you will still use CSS to help with positioning and sizing. Chapter 4 goes into more detail about CSS; this section looks specifically at the layout, using only HTML. Still the catchall container elements, the familiar s are often used to divide the page into various sections to create the layout. For example, you might see this sort of HTML used to achieve layout:

The rendering engine displays each according to its rules. To position the divisions dynamically would require CSS. The main issue with using elements to structure the document is their inability to impart standard semantic meaning to each section. You’ll revisit these examples later, when you explore creating layouts in CSS.



Objective 1.1: Create the document structure

CHAPTER 1

15

The element allows for more dynamic capability in page layout. For a more static lay­ out declared right in the HTML page, the element is more appropriate. The following HTML defines a table that provides a common blog-site format, with a header section, a left sidebar, a content area, a right sidebar, and a footer area:

The element is very flexible. Additional elements such as and provide a more semantic approach to labeling the table cells. The concern with using the element approach is the static nature of the structure. To change the overall structure of a site that uses tables for layout, you need to go to every page and make the changes. It’s worth noting that some methods that make such changes easier have evolved over the years in response to the maintenance headache involved.

Optimizing for search engines When a website is required to create an online presence, you have to ensure that it can be found among the millions of sites that already exist. Search engine optimization (SEO) is a technique used to make elements of the website easily discoverable and appropriately indexed by search engines, so that when users search for content related to your site, they find your pages. Search engines such as Bing and Google constantly scour the Internet for content. When they find webpages, they go through the HTML and index content such as page and image metadata. They use the indexed data to allow users to search for essentially anything on the Internet and receive relevant results. Clearly, then, a relationship exists between the content on your websites and how easily users can find your sites using a search engine. With the semantic HTML elements discussed in the previous objective in mind, you should note some additional things with respect to the logic search engines use to discover what’s on websites. On the whole, the subject of SEO is far outside the scope of this book and exam; entire books are written on the subject. However, discussing how HTML5 impacts SEO and website design is relevant.

16

CHAPTER 1

Implement and manipulate document structures and objects

In the past (defined as pre-HTML5), web designers often used elements to segment the page. These elements don’t provide very much context as to what they are intended to contain. But with the semantic markup available in HTML5, you can use more descriptive elements for the page sections. As you saw in the blog-page layout example, the HTML elements alone make clear the intent of each segment of the page. As search engines scour webpages, they detect the markup and know what to take from it to properly index the page. The and elements are the main ones used by the SEO algorithm. These elements are known to contain the main body of the page. That a page have more than one and/or element is acceptable; they all get indexed. Within each element, the engine then seeks out elements such as or to get the main topic of the element for relevant indexing. However, this doesn’t mean that if a site doesn’t have or elements, it won’t get indexed and be searchable. This speaks only to the quality of indexing the search engines can conduct to make your site more searchable by end users. SEO is a great technique to understand when designing websites. Creating a website just to leave it in the dark and hard to find doesn’t serve much purpose. Being found is very important and, when the site is found, you don’t want to limit your audience. Accessibility is also very important. Next, look at how HTML5 affects the use of screen readers.

Optimizing for screen readers Screen readers rely on the document outline to parse the structure and present information to the user. Screen-reader programs can read the text on the page and convert it to audio through a text-to-speech algorithm. This is helpful for users who might have difficulty viewing the webpage. As discussed earlier, the way the document gets outlined in HTML5 has changed. Here’s a little more detail. Prior to HTML5, a page was outlined using only the header elements ( through ). The relative position of each header element to the previous header element within the page created the hierarchy. Screen readers could use this information to present a table of contents to users. However, HTML5 introduced semantic elements to create new sections. This means that , , , and elements all define new sections. The introduction of the semantic elements changes how the document outline is created. For example, if the following HTML was going to be the hierarchy of the sample document, you could possibly lay it out like this: Fruits and Vegetables Fruit Round Fruit Long Fruit Vegetables Green Colorful



Objective 1.1: Create the document structure

CHAPTER 1

17

This produces the expected outline, shown in Figure 1-13.

FIGURE 1-13  The output of a series of header elements to create a document structure

The outline shows the default styles of the different header elements as expected. The heading elements create implied sections and subsections within the document. This is still valid in HTML5. However, you shouldn’t leave the page sectioning set to implied sectioning as presented by the header elements; rather, you should explicitly define the sections by using the appropriate semantics. Also recommended is that elements be used solely throughout an HTML5 document. To produce the same hierarchy in this fashion, you would need to change your HTML to be like the following: Fruits and Vegetables Fruit Round Fruit Long Fruit Vegetables Green Colorful

18

CHAPTER 1

Implement and manipulate document structures and objects

This HTML produces the same output as shown earlier in Figure 1-13. The difference is that now each element creates a new page section rather than rely on the header elements to create the sections. Screen readers can parse the semantic elements to create the document outline and eventually can provide a much richer user experience because of how HTML5 allows webpage designers to lay out the pages.

Thought experiment Converting a website to HTML5 In this thought experiment, apply what you’ve learned about this objective. You can find answers to these questions in the “Answers” section at the end of this chapter. You have just been hired to convert an existing website to the new HTML5 semantic markup. Analyze the following HTML and determine what elements should be updated to fully leverage the HTML5 semantic markup: Experimentations A Thoughtful Experiment Home Page 1 Page 2 Page 3 An Article regarding thought is presented here. Thought as a provoking element. ..................... Here are some reference material.



Objective 1.1: Create the document structure

CHAPTER 1

19

..... This page is copyright protected.

Objective summary ■■

■■

■■

■■

20

HTML5 introduced new semantic elements to more clearly define sections of an HTML page. These elements include , , , , , , , , , and . Elements within an HTML page can have their layout controlled when they are included inside structures such as elements and/or HTML tables. HTML5 semantic elements provide the mechanisms necessary to structure the page more easily for accessibility via screen readers. Search engines take advantage of HTML5 semantics by leveraging the element to determine the purpose of the page.

CHAPTER 1

Implement and manipulate document structures and objects

Objective review Answer the following questions to test your knowledge of the information in this objective. You can find the answers to these questions and explanations of why each answer choice is correct or incorrect in the “Answers” section at the end of this chapter. 1. Which of the following elements aren’t introduced in HTML5? A.



B.



C.



D.



2. Which element(s) does the element contain? A. to B.



C.



D.

All of the above

3. Which HTML5 element would you use to organize content so that the page maximizes

a search engine’s algorithm? A. B. Company News C. Company News D.

All of the above

4. Which HTML5 element should you use to create a more structured layout? A. B. C. D.



Objective 1.1: Create the document structure

CHAPTER 1

21

Objective 1.2: Write code that interacts with UI controls In this objective, you review how to interact with webpages in the browser using code. Web browsers include a powerful environment in which you can control the behavior of webpages. And some new HTML5 elements provide improved interactivity for end users. You also review how to modify the document object model dynamically, using JavaScript. You review how to implement video and audio in webpages and how to control them programmatically. Finally, you review how to render graphics dynamically or allow users to draw their own graphics.

This objective covers how to: ■■

Add or modify HTML elements

■■

Implement media controls

■■

Implement graphics with HTML5 canvas and SVG

Adding or modifying HTML elements The ability to modify an HTML document at run time is very powerful. So far you’ve seen how to create your webpages, lay them out elegantly, and render them for users. In many cases, you should modify the layout of your webpages at run time depending on what your users do. This is where you can take advantage of the power of JavaScript. JavaScript provides the toolkit you need to write code that interacts with webpage elements after they are already rendered into the browser. Before you can start to modify the webpage, you need to know how to access or reference the elements so you can manipulate them.

Document Object Model The Document Object Model (DOM) is a representation of the structure of your HTML page that you can interact with programmatically. As demonstrated earlier, an HTML page is a hierarchy. The browser produces an outline based on the HTML hierarchy presented to it and displays this in the browser to the user. Behind the scenes, unknown to the user, the browser constructs a DOM. The DOM’s application programming interface (API) is exposed as objects with properties and methods, enabling you to write JavaScript code to interact with the HTML elements rendered to the page. This notion is very powerful. You can add new elements to the page that didn’t even exist in your original HTML page. You can modify elements to change their behavior, layout,

22

CHAPTER 1

Implement and manipulate document structures and objects

appearance, and content. Theoretically, although this is rarely a recommended practice, you could render a blank HTML page to the browser, build the entire page using JavaScript, and produce the exact same results. Having that power over your webpages is very exciting, even after they are rendered. You start by selecting items in the DOM to get a reference to them.

Selecting items in the DOM To manipulate the DOM, you need to know how to access it and to obtain references to the elements you want to manipulate. In the next section you look at altering the DOM, but first you need to get elements from the DOM so you can work with them. NOTE  THE DOCUMENT OBJECT MODEL AS A FAMILY TREE

The DOM is essentially a collection of nodes arranged in a tree. All the nodes are related to each other. They are one big happy family of children, siblings, parents, grandparents, grandchildren, and so on. This essence of a family tree represents the hierarchy of the DOM and is important to understand as you manipulate the DOM through code.

You have a few choices when it comes to using JavaScript to access the DOM. You can access DOM elements through a global object provided by the browser, called document, or through the elements themselves after you obtain a reference to one. Table 1-2 outlines the core native methods used for selecting elements in the DOM. TABLE 1-2  Methods available for selecting DOM elements

Method

Usage description

getElementById

Gets an individual element on the page by its unique id attribute value

getElementsByClassName

Gets all the elements that have the specified CSS class applied to them

getElementsByTagName

Gets all the elements of the page that have the specified tag name or element name

querySelector

Gets the first child element found that matches the provided CSS selector criteria

querySelectorAll

Gets all the child elements that match the provided CSS selector criteria

For the most part, the methods are straightforward to use. In this section, you begin with a simple HTML document structure that you will use in many other examples in this book to highlight various concepts. Create a webpage with the HTML markup in Listing 1-2 to proceed with the following examples.



Objective 1.2: Write code that interacts with UI controls

CHAPTER 1

23

LISTING 1-2  HTML source to work with the DOM Main Paragraph First List Item Second List Item Third List Item Fourth List Item Paragraph 1 Paragraph 2 Paragraph 3 Paragraph 4 Row 1 Row 2 Row 3 Row 4 Row 5

This sample page is very simple, but it serves the purpose of demonstrating various ways to access elements through code. To demonstrate this functionality, you need an entry point. Add the following script block to the head section of the webpage: window.onload = function () { ... }

This should look familiar, but if it doesn’t, you’ll review the concepts later. For now, this code essentially tells the runtime to run your code after the window finishes loading. You can

24

CHAPTER 1

Implement and manipulate document structures and objects

use your code to experiment with the various methods listed in Table 1-2 in this function, starting with getElementById. The getElementById method returns the element in the page that matches the specific ID value you pass to it. It returns null if no element on the page has the specified ID. Each element on the page should have a unique ID. For example, if you want to reference the element with the ID outerDiv, you would use the following code: var element = document.getElementById("outerDiv"); alert(element.innerHTML);

The JavaScript alert method, which displays a message box, is used here to show whether you have actually accessed the DOM successfully. The alert isn’t all that useful in the real world but it is for development purposes. When you run the page, notice the message box from the browser with all the innerHTML contents of the you selected out of the DOM with your code (see Figure 1-14).

FIGURE 1-14  A JavaScript alert demonstrating successful access to the DOM



Objective 1.2: Write code that interacts with UI controls

CHAPTER 1

25

Now that you have successfully obtained a reference to your , you can do anything you want to it dynamically—just as you could have defined or applied such changes to it statically. The getElementById method is great when you know the ID of a specific element in the page that you want to work with, but in other cases you might want to do something to all the elements of a particular type—for example, all paragraph elements. In this case, the getElementsByTagName method is more appropriate. You can use the following code to get a reference to all the elements: window.onload = function () { var paragraphs = document.getElementsByTagName("p"); alert(paragraphs.length); }

In this code, the object returned from the getElementsByTagName method is a little different; it’s a special type that acts as a wrapper to all the elements that match your parameter, called a NodeList. This object isn’t especially useful by itself. In fact, it doesn’t really provide anything useful other than a length, which lets you know how many items it contains, and the ability to access each individual item. In the preceding example, the JavaScript alert displays how many items were returned in the list (paragraphs.length). You can see that the method returned all five of the elements in the page, as shown in Figure 1-15.

FIGURE 1-15  A message showing the number of elements

In the same way that you could use the getElementsByTagName method to get all elements of the same type, you can use the getElementsByClassName method to get all elements of the same CSS class. This is useful when you have many elements with the same style but perhaps want to modify them at run time. This method also returns a NodeList. The following snippet demonstrates the usage: window.onload = function () { var paragraphs = document.getElementsByClassName("subPara"); alert(" elements with class subPara: " + paragraphs.length); }

Figure 1-16 shows the output of this script.

26

CHAPTER 1

Implement and manipulate document structures and objects

FIGURE 1-16  A message showing the number of elements with the specified class name subPara

This example adds a little more text to the message box so that it looks different from the previous example, but the idea is the same. All elements with the subPara class assigned to them were returned in a NodeList. You can see that the call returned four HTML elements. When selecting elements in the DOM by class name, the NodeList contains all elements whose class matches the specified class—not just elements of the same type. If, for example, you assigned the class subPara to one of your elements and then ran the function again, the returned NodeList would contain the four elements and the element because they all have the same class. This is important when you intend to iterate over the elements and do something to them. In Figure 1-17, the same JavaScript code is run, but with an added subPara class attribute to a element.

FIGURE 1-17  The same script run with a assigned the class name subPara

This message box is now actually incorrect, because the NodeList contains a single element and the four elements. Keep this behavior in mind concerning the getElementsByClassName method. All the methods you have looked at so far to find elements in the DOM provide a specific implementation for a specific purpose. If you want a single element by its unique ID, you use the getElementById method; if you want to find an element or all the elements of a specific CSS class, you use the getElementsByClassName method. Now look at some examples that use the much more flexible querySelector and querySelectorAll methods. The querySelector and querySelectorAll methods allow you to achieve most of what you’ve already done with the other methods. Both methods take a parameter in the form of a CSS

Objective 1.2: Write code that interacts with UI controls

CHAPTER 1

27

selector. The querySelector method returns the first element it finds that matches the selector criteria passed to it, whereas the querySelectorAll method returns all elements that match the selector criteria passed in. The elements are still returned in the form of a NodeList object. Both methods exist not only on the document itself, but also on each element. Therefore, when you have a reference to an element, you can use these methods to search its children without having to traverse the entire document. You can see some simpler examples in this section. To find all the elements on a page, you can use this syntax: document.querySelectorAll("p");

To find an element by its unique ID, you can use this syntax: document.querySelector("#outerDiv");

Put those two lines into your HTML file and try them out. You will explore much more advanced and interesting functionality in Chapter 4. For now, you can use what you’ve seen about finding elements in the DOM to apply that knowledge to adding or modifying the DOM through code. EXAM TIP

jQuery is probably the most popular library available to date for simplifying and extending the core JavaScript capabilities. Although jQuery isn’t a Microsoft technology, it’s essentially an industry standard and fully supported by Microsoft. As such, web developers today are generally understood to have a grasp of using jQuery interchangeably with core JavaScript. The exam will expect that you can use jQuery effectively in place of the document object selector methods.

Altering the DOM Having access to the DOM through JavaScript can be used to provide rich user experience when creating dynamic webpages. So far, all you’ve done is obtain references to the elements, which is not particularly useful by itself. The purpose of retrieving elements from the DOM is to be able to do something with them. In this section, you look at how to manipulate the DOM by using JavaScript code to add and remove items. After you have a reference to a container element, you can add child elements to it dynamically. You can remove elements from it or simply hide elements. When you remove an element from the DOM, it is gone. So if you want to make something invisible to the user but

28

CHAPTER 1

Implement and manipulate document structures and objects

be able to use it again later, you can simply hide it by using the appropriate CSS rather than remove it. Here’s an example: var element = document.getElementById("innerDiv"); alert(element.innerHTML); document.removeChild(element); var afterRemove = document.getElementById("innerDiv"); alert(afterRemove);

The first alert properly shows the innerHTML property of the innerDiv, but the code never reaches the second alert. Instead, the getElementById method throws an error because the element id specified no longer exists in the document. Be aware of various methods when it comes to adding elements to and removing them from the DOM. The first method to look at is document.createElement. You use this method of the document object to create a new HTML element. The method receives a single parameter— the element name of the element you want to create. The following code creates a new element to use in your page: var element = document.createElement("article"); element.innerText = "My new element";

This new element isn’t visible to anyone at this point; it merely exists in the DOM for use within your page. Because you don’t have much need to create elements but then not use them, next look at the methods available to get your new element into your page. The first of these methods is appendChild. You use this method to add a new HTML element to the collection of child elements belonging to the calling container. The node is added to the end of the list of children the parent node already contains. The appendChild method exists on the document object as well as on other HTML container elements. It returns a reference to the newly added node. This example appends a new element to the outerDiv: var outerDiv = document.getElementById("outerDiv"); var element = document.createElement("article"); element.innerText = "My new element"; outerDiv.appendChild(element);

Like most of the other methods explained in this section, the appendChild method returns a reference to the new element appended to the child elements. This is a good way to ensure that you always have a reference to an element for future use, especially when deleting elements. It also enables you to simplify or restructure the code. The following code achieves the same result: var element = document.getElementById("outerDiv").appendChild(document. createElement("article")); element.innerText = "My new element";



Objective 1.2: Write code that interacts with UI controls

CHAPTER 1

29

Figure 1-18 shows the output of this code.

FIGURE 1-18  A new element appended to the bottom of the page

You can see that the element was put in at the end of your page. The appendChild method always adds the new element to the end of the parent element’s child node list. To insert the new element somewhere more precise, the insertBefore method could be more suitable. This method takes two parameters: the new element itself, and the node before which you want to append the new element. For example, to insert your new article before the innerDiv element, you could write the following code: var element = document.getElementById("outerDiv").insertBefore( document.createElement("article"), document.getElementById("innerDiv")); element.innerText = "My new element";

This example uses the getElementById method to get a reference to the node before which you wanted to insert your element in the DOM. You can use other tools to make this code simpler in some cases, depending on the document’s structure. Each element or node has the properties listed in Table 1-3 to help get references to the more common nodes when working with the DOM.

30

CHAPTER 1

Implement and manipulate document structures and objects

TABLE 1-3 Properties available on a DOM element

Property

Description

childNodes

A collection of all child nodes of the parent element.

firstChild

A reference to the very first child node in the list of child nodes of the parent node.

lastChild

A reference to the very last child node in the list of the child nodes of the parent node.

hasChildNodes

A useful property that returns true if the parent element has any child nodes at all. A good practice is to check this property before accessing other properties, such as firstChild or lastChild.

For an example of these properties, you can change the preceding code to insert your element as the first element in the innerDiv element: var inner = document.getElementById("innerDiv"); var element = inner.insertBefore(document.createElement("article"),inner.firstChild); element.innerText = "My new element";

This code produces the output shown in Figure 1-19.

FIGURE 1-19  The new element inserted as the first child of a element

Your element is now positioned as the first child element of the innerDiv element. Experiment with the other properties to become familiar with how they behave. Every element that can have child elements supports all this functionality; however, if you try to insert elements into a node that doesn’t support child nodes—such as an , for example—the interpreter throws a run-time error.

Objective 1.2: Write code that interacts with UI controls

CHAPTER 1

31

Just as you can add new elements to the DOM through code, you also can remove elements from the DOM using code. In this section you look at the methods available to do just this, named removeChild and removeNode. The removeChild method removes a child node from the calling container. This method exists on the document object as well as other HTML container elements. The removeChild method returns a reference to the removed node. This is especially handy if you plan to return that node to the DOM—perhaps in response to some other user interaction with the page. Remember, however, that if you don’t keep the returned reference to the removed node, you have no way to add the element back in without completely re-creating it. The following example removes the first element from your innerDiv element: var innerDiv = document.getElementById("innerDiv"); var p = innerDiv.removeChild(document.getElementById("P1"));

This code provides the output in Figure 1-20. You can see that the first element has been removed. Because you captured the removed element into the variable p, you could use it later if you wanted to put the element somewhere else.

FIGURE 1-20  Removal of the first element by the removeChild method

Another useful method for removing nodes or elements is removeNode, which takes one Boolean parameter. Setting the parameter as true tells the method to do a deep removal, which means that all children are also removed. The following code demonstrates this: var innerDiv = document.getElementById("innerDiv"); innerDiv.removeNode(true);

32

CHAPTER 1

Implement and manipulate document structures and objects

Figure 1-21 shows that when this code is run in the browser, the innerDiv element has been removed.

FIGURE 1-21  Using the removeNode method to remove the node

Now, suppose that you want to change the content of the page more dramatically—perhaps even by rewriting all the HTML content. This is completely possible with the techniques you have seen so far, but you haven’t tried some methods yet: replaceNode and replaceChild. These two methods operate in the same way as removeNode and removeChild in terms of the parameters they take and which elements they affect. The difference, however, is that you can replace the target element with a completely new element. The following code converts all your inner paragraphs to anchor elements and adds line breaks, because you don’t get those automatically as you do from the element: var innerDiv = document.getElementById("innerDiv"); var newDiv = document.createElement("div"); for (var i = 0; i < innerDiv.childNodes.length; i++) { var anchor = newDiv.appendChild(document.createElement("a")); anchor.setAttribute("href", "http://www.bing.ca"); anchor.text = innerDiv.childNodes[i].textContent; newDiv.appendChild(document.createElement("br")); } innerDiv.replaceNode(newDiv);

This code produces the browser output as shown in Figure 1-22.



Objective 1.2: Write code that interacts with UI controls

CHAPTER 1

33

FIGURE 1-22  Converting all the elements to elements

All your plain-text paragraphs now display as hyperlinks. Your original innerDiv element is gone and no longer in the DOM. Your only reference to it is within the JavaScript code. You would need to hang on to that reference if you intended to swap it back into the DOM later. Further, because the code didn’t assign the new element a unique id, the only way to get a reference to it in the DOM is through your existing code reference. For this reason, a recommended practice is to always give your new elements a unique id. If the JavaScript variables go out of scope before you insert them into the document, you lose the references to your elements completely. In this section, you saw how to access HTML elements by using JavaScript to manipulate the DOM in the browser. You now can retrieve references to the elements or nodes that make up your HTML document as well as modify, add, and remove elements in the HTML document. Next, you look at implementing media controls into your pages.

Implementing media controls Embedding multimedia elements into webpages isn’t a new concept. This capability has been around for a long time and has presented challenges in various situations. One key challenge has often been dependence on a third-party object integrated with the browser to render the media. In this section, you look at two new elements added to the HTML5 specification that work with multimedia natively in the web browser and with JavaScript. You also examine the and elements.

34

CHAPTER 1

Implement and manipulate document structures and objects

Using the element Embedding video into a webpage has become very popular, and many websites now include a video element in their design. HTML5 has made including video in your webpages much easier than it was previously. Here, you learn about the new element provided by the HTML5 standard and look at the available attributes and events you can use to control video either declaratively, through static HTML, or dynamically, using JavaScript. Embedding a video in the page is as simple as adding the following markup:

That’s the bare minimum. However, you know that the bare minimum is rarely enough for a professionally designed website. You need to work with more properties and events. You also need to consider browser support for various video formats. First, you need to examine the key attributes available to use on the element, as listed in Table 1-4. TABLE 1-4 Attributes available on the element

Attribute

Description

src

This attribute specifies the video to play. It can be a local resource within your own website or something exposed through a public URL on the Internet.

autoplay

This attribute tells the browser to start playing the video as soon as it loads. If this attribute is omitted, the video plays only when told to through player controls or JavaScript.

controls

This attribute tells the browser to include its built-in video controls, such as play and pause. If this is omitted, the user has no visible way to play the content. You would use autoplay or provide some other mechanism through JavaScript to play the video.

height/width

These attributes control the amount of space the video will occupy on the page. Omitting these causes the video to display in its native size.

loop

This attribute tells the browser to continuously play the video after it completes. If this attribute is omitted, the video stops after it plays through completely.

poster

This attribute specifies an image to show in the place allocated to the video until the user starts to play the video. Use this when you’re not using autoplay. It’s very useful for providing a professional image or artwork to represent the video. If it’s omitted, the poster appears in the first frame of the video.

With all this new information about the available attributes, you can provide a bit more detail in your element to control how you would like it to behave:

The preceding element specifies that it should initially display a poster image, sets the height and width parameters, and indicates that the default controls should be available. The absence of a loop attribute means that when the video is finished, it shouldn’t repeat automatically. And the absence of an autoplay attribute tells the browser that you don’t want the video to start playing automatically; instead, it should wait until the user invokes the play



Objective 1.2: Write code that interacts with UI controls

CHAPTER 1

35

operation with the controls or until you invoke the play operation with JavaScript. When you do include the default controls, the user gets a basic set. Figure 1-23 shows what the default controls look like in Internet Explorer.

FIGURE 1-23  The default Internet Explorer media controls

From left to right, the default controls provide a play button that changes to a pause button while the video is playing. A timer shows the current video position and how much time remains in the video. A slider bar lets users navigate to a specific point in the video. The audio control button pops out a volume slider bar when pressed, and finally, at the far right, is a control that enables users to display the video at full-screen size. So far, so good—for Internet Explorer users. But you also need to ensure that your video will play successfully in other browsers. The problem is that not all browsers support all video formats. Keep this in mind as you implement your elements; what each browser supports can (and will) change as well. You need to ensure that you provide options to the browser so that it can choose which video format to play. If you don’t have all the appropriate supported video formats and your page happens to get a visitor with a browser that can’t play the video format you have, you also need to provide an alternative or at least the information that the user’s browser doesn’t support this video. The following code demonstrates this: Video is not supported by this browser.

This sample removed the src attribute from the element and added child elements instead. The element supports multiple elements, so you can include one for each video type. A browser goes through the elements from top to bottom and plays the first one that it supports. Notice that the example also has an element to cover the possibility that the client browser has no support for the element at all. In such cases, you could have a Flash version of the video to play; but if no other version of the video is available to play, you can just display a message that video isn’t supported, as shown in the code snippet. Browsers that don’t support the element ignore the element altogether but show the element that they do understand. This lets older browsers “fall back” to previous methods for displaying video, ensuring that you can reach as many users as possible. Finally, the element is a last resort to provide at least some information to users that a video is supposed to be playing here but that their browser doesn’t support it.

36

CHAPTER 1

Implement and manipulate document structures and objects

EXAM TIP

If the browser supports the HTML5 video element, it doesn’t show the fallback. In this case, make sure that you have the valid element specified for that browser. If you don’t, the video container shows an error in place of the control bar, saying that an invalid link or file is specified.

Sometimes having more control over things is nice, or perhaps you just don’t like the look and feel of the default controls. This is where JavaScript comes in. You can create your own control bar and substitute your own control buttons to enable users to control the video. The following example adds a few custom image elements to the page and wires up some JavaScript to control the video: img:hover { cursor: pointer; } var video; window.onload = function () { video = document.getElementById("sampleVideo"); } function play() { video.play(); } function pause() { video.pause(); } function back() { video.currentTime -= 10; }



Objective 1.2: Write code that interacts with UI controls

CHAPTER 1

37

This HTML produces the media controls shown in Figure 1-24.

FIGURE 1-24  A custom media control bar

As you can see, the code has created a little custom control bar and positioned it to the right of the video frame. The element offers many methods. Table 1-5 outlines the more common ones. TABLE 1-5 Methods and properties on the object

Method/property

Description

play()

Plays the video from its current position.

pause()

Pauses the video at its current position.

volume

Allows the user to control the volume of the video.

currentTime

Represents the current position of the video. Increase or descrease this value to move forward or backward in the video.

You’ve learned all about how to display video in your webpages. Now turn your attention to playing sounds using the element.

Using the element The element is essentially identical to the element. It has all the same attributes and the same methods. The only real difference is how it displays in the browser. Because no video is available to show, the element occupies no screen space. However, you can show the default controls—or you can again choose not to show the default controls and to create your own mechanism to control the audio, either through custom user

38

CHAPTER 1

Implement and manipulate document structures and objects

interface elements or behind the scenes in JavaScript. Here is an example of what an declaration looks like in your webpage: Your browser does not support HTML5 audio.

This HTML provides the output in Internet Explorer shown in Figure 1-25.

FIGURE 1-25  The default audio controls in Internet Explorer

Figure 1-25 shows the output of the element when you opt to use the built-in controls. From left to right, you get a pause/play button, the counter, a progress bar, the total time in the audio, and a volume slider bar. Because no other screen space is required as in the video samples, the element has no height or width properties available. If you don’t like the built-in audio control bar, you can choose not to include it in your declaration and instead create a custom control bar that suits your needs. The and elements are very similar. The key point regarding these elements is that they provide a standardized way to represent media in HTML pages to simplify reading the HTML code and know exactly what the page is supposed to be doing. Now that you know how to use audio and video in your webpages, you can turn your attention to the use of graphics.

Implementing graphics with HTML5 and SVG HTML5 provides a new mechanism to work with graphics in your webpages. The HTML5 specification introduces the webpage element, which provides a blank canvas on which you can draw dynamically. You can draw lines, text, and images on the canvas and manipulate them with JavaScript. Adding a canvas to your page is as simple as declaring one in the HTML. The element is similar to the element. However, it’s a container for graphics as opposed to text-based elements. Here is the markup for a element: Your browser does not support HTML5.

The HTML is very straightforward. You simply need to define a and specify a size. Also, if the user’s browser doesn’t support the element, you can place fallback text inside the element to be displayed in its place. When you run this HTML in the browser, you should notice absolutely nothing! This is because—just like with a element or any other container—the element has no default visibility; in other words,



Objective 1.2: Write code that interacts with UI controls

CHAPTER 1

39

it’s visible, but it’s white with no borders, and thus it’s invisible on a blank HTML page. The next example adds a simple style to your element so that you can see its borders: canvas { border: 1px solid black; }

Now you can see your canvas, which should look Figure 1-26.

FIGURE 1-26  A blank element

A blank canvas isn’t terribly exciting yet. But now that you have a basic canvas up and running, you can work through all the various methods to create graphics on the canvas. To do that, you should create an onload event for your window (as you have in previous examples) to encapsulate your code and cause the graphics to render when the page is loaded. To draw on the canvas, you need to understand the coordinate system that the canvas uses. The canvas provides a fixed (x,y) coordinate system in which the top-left corner of the canvas is (0,0). In this case, the bottom-left corner of the canvas is (0,400), the top-right corner is (600,0), and the bottom-right corner is (600,400). You should be fairly used to this type of system because it matches the browser window coordinate system, with (0,0) in the top-left corner. However, the position of the canvas in the browser window is irrelevant to the drawing methods you use to draw on the canvas. The coordinates for drawing on the canvas are always based on the coordinates within the canvas itself, where the top-left pixel is (0,0). As with any HTML element, to work with it through code you need to get a reference to it in your JavaScript. Begin by writing the following code in your page: window.onload = function () { var drawingSurface = document.getElementById("drawingSurface"); var ctxt = drawingSurface.getContext("2d"); }

40

CHAPTER 1

Implement and manipulate document structures and objects

In the preceding code, you get a reference to your canvas element followed by a reference to a “2d” context. The context is an object that provides the API methods you use to draw on the canvas. Now, supports only a 2d context, but you can expect to see a 3d context in the future. Having acquired a reference to the context, you can now start to look at the various methods for drawing on your canvas.

Drawing lines At the most basic level, you can draw lines on the canvas with the 2d context object you are referencing. The context object provides the following methods for drawing lines, as listed in Table 1-6. TABLE 1-6  Methods for drawing lines

Method

Description

beginPath

Resets/begins a new drawing path

moveTo

Moves the context to the point set in the beginPath method

lineTo

Sets the destination end point for the line

stroke

Strokes the line, which makes the line visible

With that information, in its simplest form, you can draw a line across your canvas like this: ctxt.beginPath(); ctxt.moveTo(10, 10); ctxt.lineTo(225, 350); ctxt.stroke();

This code produces the line on the canvas shown in Figure 1-27.

FIGURE 1-27  A line drawn on the canvas



Objective 1.2: Write code that interacts with UI controls

CHAPTER 1

41

At the point where the line ends, you can continue drawing more lines by adding more lineTo methods, as in the following example: ctxt.beginPath(); ctxt.moveTo(10, 10); ctxt.lineTo(225, 350); ctxt.lineTo(300, 10); ctxt.lineTo(400, 350); ctxt.stroke();

Run this code and look at the output. You get a graphic that resembles Figure 1-28. You might use straight lines in this way when plotting connected points on a line graph, for example.

FIGURE 1-28 A polyline drawn on the canvas

Exploring the stroke method in more depth is worth the effort. If you were creating a chart or a graph, you might want to change the color of your lines so that they stand out from the axis. You might want to change the thickness. You do this by changing some properties on the context object before calling the stroke method: ctxt.lineWidth = 5; ctxt.strokeStyle = '#0f0';

The lineWidth property accepts a value that determines the line width. The strokeStyle property lets you change the line color. This property accepts all the common style formats for specifying colors in HTML, including hexadecimal values or named colors. These changes produce a new, more attractive output, as shown in Figure 1-29.

42

CHAPTER 1

Implement and manipulate document structures and objects

FIGURE 1-29  The polyline rendered with a different color

You also can experiment with the lineCap property, which accepts a few values that control how the end of the line will render. For example, you can set the lineCap property to round to give the line a rounded cap. Applying a cap affects the length of the line. The cap is added to the end of line, and its length matches what you have set for the line’s width. In this example, the line has a width of 5. With a round cap set on the line, the total line length would be extended by 5. You’ve seen all you need to know about working with straight lines. Now turn your attention to curves.

Drawing curves Drawing curves is a little more involved because you have more parameters to consider. Table 1-7 lists the methods used when working with curves on the HTML5 canvas. TABLE 1-7  Methods for drawing curves

Method

Description

arc

A standard arc based on a starting and ending angle and a defined radius

quadradicCurveTo

A more complex arc that allows you to control the steepness of the curve

bezierCurveTo

Another complex arc that you can skew

Each drawing method can have styles applied to it, just like the line examples. You can control the lineWidth, strokeStyle, and lineCap properties to change how your curves display. Start off by creating some basic arcs on your canvas. The arc method takes the parameters listed in Table 1-8.



Objective 1.2: Write code that interacts with UI controls

CHAPTER 1

43

TABLE 1-8 Parameters required to draw an arc

Parameter

Description

X, Y

The first two parameters are the X and Y coordinates for the center of the circle.

radius

The third parameter is the radius. This is the length of the distance from the center point of the circle to the curve.

startAngle, endAngle

The fourth and fifth parameters specify the starting and ending angles of the arc to be drawn. This is measured in radians, not in degrees.

counterclockwise

The final parameter specifies the drawing direction of the arc.

Add the following code to your page: ctxt.beginPath(); ctxt.arc(150,100,75,0,2 * Math.PI, false); ctxt.lineWidth = 25; ctxt.strokeStyle = '#0f0'; ctxt.stroke(); ctxt.beginPath(); ctxt.arc(450, 100, 75, 1.5 * Math.PI, 2 * Math.PI, false); ctxt.lineWidth = 25; ctxt.strokeStyle = 'blue'; ctxt.stroke(); ctxt.beginPath(); ctxt.arc(150, 300, 75, 1 * Math.PI, 1.5 * Math.PI, false); ctxt.lineWidth = 25; ctxt.strokeStyle = '#0ff'; ctxt.stroke(); ctxt.beginPath(); ctxt.arc(450, 300, 75, .5 * Math.PI, 1 * Math.PI, false); ctxt.lineWidth = 25; ctxt.strokeStyle = '#f00'; ctxt.stroke();

This code sample draws four arcs: a full circle followed by three quarter circles, each with a different style. Notice that some math formulas are specified in the parameters to the arc. This math is necessary to get the value in radians because the parameters for startAngle and endAngle are specified in radians, not in degrees. The code produces the drawing shown in Figure 1-30.

44

CHAPTER 1

Implement and manipulate document structures and objects

FIGURE 1-30  Drawing arcs on the canvas in different colors

The previous example demonstrated a simple arc. Now look at the next arc method, the quadraticArc. The curve of a quadratic arc is evenly distributed from one end to the other in terms of its distance from the center point. The quadraticCurveTo method allows you to specify some additional parameters to alter the “steepness” of the curve—in other words, to change the distance from the center point along the curve. Drawing a quadratic curve is somewhat like drawing a straight line but then pinching it in the middle and pulling it away to create a curve where the starting and ending points of the line stay fixed. The farther away you pull the center point, the steeper the curve becomes. Here’s an example: ctxt.beginPath(); ctxt.moveTo(10,380); ctxt.quadraticCurveTo(300,-250,580,380); ctxt.lineWidth = 25; ctxt.strokeStyle = '#f00'; ctxt.stroke();

You first need use the moveTo method to tell the context where you want your curve to start. Then, you pass the four parameters described in Table 1-9 to the quadraticCurveTo method. TABLE 1-9  Parameters required for the quadraticCurveTo method

Parameter

Description

controlX, controlY

These parameters define the control point, relative to the top left of the canvas, that is used to “stretch” the curve away from the line formed by the start and end points.

endX, endY

This is the point where the curve should end.

The code sample produces the image in Figure 1-31.



Objective 1.2: Write code that interacts with UI controls

CHAPTER 1

45

FIGURE 1-31  A quadratic curve output onto a canvas

As the control point moves farther away from the line formed by the start and end points, you get a steeper curve. This example used a negative number to indicate that the control point should be above the top of your canvas to stretch the curve to where you want it. The final curve to look at is the Bezier curve. A Bezier curve is similar to the quadratic curve except that it has two control points instead of just one. Having two points allows the Bezier curve to create more complex curves. In both examples you have seen so far, the curve was created around the context of a single point. The Bezier curve changes that. It’s easiest to see in an example, and then I’ll explain the parameters. Create the following code: ctxt.beginPath(); ctxt.moveTo(125, 20); ctxt.bezierCurveTo(0, 200, 300, 300, 50, 400); ctxt.lineWidth = 5; ctxt.strokeStyle = '#f00'; ctxt.stroke();

The bezierCurveTo method follows a moveTo method call in the same way that the quadraticCurveTo method did. You need to pass three sets of coordinates to the bezierCurveTo method, as listed in Table 1-10. TABLE 1-10  Parameters required for the bezierCurveTo method

46

Parameter

Description

controlX, controlY

The first two parameters specify the first control point that is used to stretch out the curve.

Control2X, control2Y

The second two parameters specify the second control point that is used to stretch out the curve.

endX, endY

The final two parameters specify the end point for the curve.

CHAPTER 1

Implement and manipulate document structures and objects

The code sample produces the output shown in Figure 1-32. You can see that this curve is skewed because of the two control points.

FIGURE 1-32  A Bezier curve drawn on a canvas

In the next section, you learn about using path methods to combine everything you’ve looked at so far.

Using path methods When using the context object to draw, you always need a starting point and an ending point. The ending point for one stroke also can become the starting point for the next stroke. You do this by calling the beginPath method on the context object and then drawing all your lines before calling either the closePath method (which ends the line) or the beginPath method (which starts a new line) again. Recall that the first arc example called the beginPath method before drawing each arc. Had the code not done that, the line would have continued across the canvas from one arc to the next. But by calling the beginPath method again, you reset the path’s starting point. So essentially, you can string together all the calls to the various drawing methods to create a complex stroke. The stroke—no matter how simple or complex—is called a path. Run the following code and see what kind of image you end up with: ctxt.beginPath(); ctxt.arc(300, 200, 75, 1.75 * Math.PI, 1.25 * Math.PI, false); ctxt.lineTo(150, 125); ctxt.quadraticCurveTo(300, 0, 450, 125); ctxt.lineTo(353, 144); ctxt.strokeStyle = "blue"; ctxt.lineCap = "round"; ctxt.lineWidth = 10; ctxt.stroke();

This code produces the output shown in Figure 1-33.

Objective 1.2: Write code that interacts with UI controls

CHAPTER 1

47

FIGURE 1-33  A custom path drawn on the canvas

Although this is still a simple example, you can get creative and put together images quite nicely. Depending on your math skills, you can create some very complex graphics using these methods. Next, look at the other methods that exist for drawing shapes.

Using the rect method Earlier, you saw how to draw circles using the arc method. And as you saw in the previous section, you can draw custom shapes of any sort and size by using the beginPath method and stringing together a series of drawing methods. But you don’t always need to do that; some shapes are built in. In this section, you look at the built-in functionality to build rectangles. Then you look at the functionality to fill your drawn shapes with colors and patterns. The context object to which you have a reference has a method called rect. The rect method takes the parameters listed in Table 1-11. TABLE 1-11  Parameters required for drawing rectangles

Parameter

Description

x,y

The x-coordinate and y-coordinate define the starting position of the rectangle. This is the top-left corner of the rectangle.

width

This defines the width of the rectangle.

height

This defines the height of the rectangle.

A simple call to the rect method, as in the following code, draws a rectangle: ctxt.beginPath(); ctxt.rect(300, 200, 150, 75); ctxt.stroke();

This code draws a rectangle as shown in Figure 1-34.

48

CHAPTER 1

Implement and manipulate document structures and objects

FIGURE 1-34  A rectangle drawn on the canvas using the rect method

Notice that although the parameters passed in for the top-left corner were (300,200), which is the center of your canvas, the rectangle is off center. To center your rectangle, you would need to do a bit of math to calculate the center based on the size of your canvas as well as the size of your desired rectangle. The following code should center your rectangle: ctxt.beginPath(); var x, y; x = 150; y = 75; ctxt.rect(300—(x/2), 200—(y/2), x, y); ctxt.stroke();

Now that you can draw shapes and rectangles, you can look at how you would go about filling your shapes with colors or patterns.

Using the fill method In this section you examine how you can fill your shapes. You have drawn various types of shapes, but so far they have been empty. Here you’ll see how to fill them with colors, gradients, and patterns. Filling a shape with a color is as simple as setting the fillStyle property to a color and calling the fill method. Inserting the following code before calling the stroke method fills your shape with a blue color: ctxt.fillStyle = "blue"; ctxt.fill();

With respect to the rect method, you get a special fill method specifically for rect called fillRect. With this method, you can create and fill your rectangle in one call: ctxt.fillStyle = "blue"; ctxt.fillRect(300—(x / 2), 200—(y / 2), x, y);



Objective 1.2: Write code that interacts with UI controls

CHAPTER 1

49

Using the fillRect method reduces the amount of code required. And it’s just as simple to fill nonrectangular shapes, such as the complex Path graphic that you created earlier, with a single call to the fill method: ctxt.beginPath(); ctxt.arc(300, 200, 75, 1.75 * Math.PI, 1.25 * Math.PI, false); ctxt.lineTo(150, 125); ctxt.quadraticCurveTo(300, 0, 450, 125); ctxt.lineTo(353, 144); ctxt.strokeStyle = "blue"; ctxt.lineCap = "round"; ctxt.lineWidth = 10; ctxt.fillStyle = "Green"; ctxt.fill(); ctxt.stroke();

You can see in Figure 1-35 that the logic of coloring in this complex shape is completely handled by the browser.

FIGURE 1-35  Using the fill method to color in a complex object

That’s all it takes to fill a shape with a solid color. Filling shapes with a gradient requires a few extra steps. Creating a gradient involves using a new CanvasGradient object. You first call the createLinearGradient method available on the context object to get a CanvasGradient object. On that CanvasGradient object, you define the color stops that you want to blend to create the gradient effect. Then you assign your CanvasGradient object to the fillStyle property of the context. The following code creates and fills a rectangle with a linear gradient: var ctxt = drawingSurface.getContext("2d"); ctxt.lineWidth = 3; ctxt.rect(150, 150, 200, 125); var gradient = ctxt.createLinearGradient(150, 150, 200, 125); gradient.addColorStop(0, "Black"); gradient.addColorStop(0.5, "Gray"); gradient.addColorStop(1,"White"); ctxt.fillStyle = gradient; ctxt.fill(); ctxt.stroke();

50

CHAPTER 1

Implement and manipulate document structures and objects

This code creates the CanvasGradient object by passing in the start and end points of a gradient line. You then add three color stops. The addColorStop method takes two parameters. The first is a value from 0 to 1, where 0 is the starting point of the gradient line and 1 is the ending point. The second parameter is the color to start filling with at that stop. This example has three stops, so the gradient transitions through three colors. The gradient output is displayed in Figure 1-36.

FIGURE 1-36 The element colored with a linear gradient

You can also create a radial gradient using the createRadialGradient method. This method takes six parameters, which specify the center point and radius of two circles and the color transitions through the stops along the cone formed by the two circles. The following code produces a radial gradient in which the cone is pointed toward the viewer: var ctxt = drawingSurface.getContext("2d"); ctxt.lineWidth = 3; ctxt.rect(150, 150, 250, 175); var gradient = ctxt.createRadialGradient(200, 200,5, 250, 250,100); gradient.addColorStop(0, "Red"); gradient.addColorStop(.5, "Orange"); gradient.addColorStop(1, "Blue"); ctxt.fillStyle = gradient; ctxt.fill(); ctxt.stroke();

Figure 1-37 shows the output of this gradient:

FIGURE 1-37  A radial gradient colored on a canvas



Objective 1.2: Write code that interacts with UI controls

CHAPTER 1

51

The last fill option to look at involves using a fill pattern. You need an external image, which is applied as a pattern throughout the shape. For example, you can use a texture that you created as a background to your canvas by using the following code: var ctxt = drawingSurface.getContext("2d"); ctxt.lineWidth = 3; ctxt.rect(150, 150, 200, 125); var img = new Image(); img.src = "texture.png"; img.onload = function () { var pat = ctxt.createPattern(img, "repeat"); ctxt.fillStyle = pat; ctxt.fill(); ctxt.stroke(); }

Figure 1-38 shows the output of this code.

FIGURE 1-38  The canvas filled with a pattern drawn on it

The preceding code calls the createPattern method and passes it a reference to an Image object and a repeat pattern. The repeat pattern can be no-repeat, repeat-x, or repeat-y, but it defaults to repeat if you don’t specify anything. You need to assign an event handler to the onload event of the Image object to ensure that you draw the pattern only after the image loads. Otherwise, the code could run before the picture is rendered, and the pattern won’t display. This section has covered a lot about how to work with shapes and fill them. All your graphics have been created by using code to manually draw shapes. Next, you see how to draw existing graphics from external files on your canvas, and then you look at drawing text.

Drawing images Drawing images on a canvas is just as straightforward as the other drawing methods you’ve seen. To draw an image on a canvas, you use the drawImage method of the context object. This method takes an Image object and some (x,y) coordinates to define where the image should be drawn. Just like with the rectangle, the image’s top-left corner is drawn at the specified (x,y). The default size of the image is the actual image size, but as you will see right

52

CHAPTER 1

Implement and manipulate document structures and objects

after, you can also resize the image as you draw it. To simply draw the image, create the following code: var drawingSurface = document.getElementById("drawingSurface"); var ctxt = drawingSurface.getContext("2d"); var img = new Image(); img.src = "orange.jpg"; img.onload = function () { ctxt.drawImage(img, 0, 0); ctxt.stroke(); }

This code produces a element with the image drawn on it, as shown in Figure 1-39.

FIGURE 1-39  An image drawn on a canvas

If you want to resize the image, you can replace the drawImage method call with the following line: ctxt.drawImage(img, 0,0,img.width * .5, img.height * .5);

This reduces the image size by 50 percent. Now look at how you can draw text on your canvas.

Drawing text Drawing text on the canvas involves adding a few additional tools to your chest from the context object, using the strokeText method and the font property. You see how to apply color to your text and, finally, how to manage its alignment. In its simplest form, drawing text requires only the following code: ctxt.strokeText("1. Text with default font", 100, 100);



Objective 1.2: Write code that interacts with UI controls

CHAPTER 1

53

Remember that you need to make sure the window has finished loading and you need to get a context object. Here’s the full code for this example: window.onload = function () { var drawingSurface = document.getElementById("drawingSurface"); var ctxt = drawingSurface.getContext("2d"); ctxt.strokeText("1. Text with default font", 100, 100); }

That’s it. The strokeText call draws the specified text into the specified coordinates on the canvas. The parameters specify what text to draw, and the (x,y) coordinates specify where drawing should begin. The strokeText method draws in the default font style. You can easily change the font property of the context object to enhance the appearance of your text. For example, running the following code changes the font size to 24 and the font family to Arial: ctxt.font = "24px arial"; ctxt.strokeText("2. Text with altered font", 100, 125);

To color your text, you could add this code: ctxt.font = "24px arial"; ctxt.strokeStyle = "Red"; ctxt.strokeText("3. Text with altered colored font", 100, 160);

When you run the preceding code, notice that your text is outlined. This is the default behavior when you increase the font size; it’s drawn as outlined. To draw solid-colored text, add the following code, which sets the fillStyle property and calls the fillText method instead of the strokeStyle and StrokeText methods: ctxt.font = "24px arial"; ctxt.fillStyle = "Red"; ctxt.fillText("4. Text with altered colored font", 100, 185);

You can also set the alignment of your text within the canvas. For example, to ensure your text is centered, add this code: ctxt.font = "24px arial"; ctxt.textAlign = "center"; ctxt.fillStyle = "Red"; ctxt.fillText("5. Text with altered colored font Centered.", drawingSurface.width / 2, drawingSurface.height / 2);

By setting the textAlign property to the value center, you are telling the context to consider the specified (x,y) coordinate as the center point of the string instead of the beginning point of the string. So, you divide the canvas width and height by two to get the center point of the canvas, and you get a string centered horizontally and vertically. Figure 1-40 shows the progression of your text:

54

CHAPTER 1

Implement and manipulate document structures and objects

FIGURE 1-40  Progression of text with changing styles

The canvas is a strong utility for presenting graphics dynamically in the browser. However, it’s not the only graphical tool available. In the next section, you look at using Scalable Vector Graphics.

Scalable Vector Graphics (SVG) Scalable Vector Graphics (SVG) is an XML-based language for creating two-dimensional graphics. It’s implemented by using tags defined by the SVG XML namespace and embedded in HTML5 documents within opening and closing elements. SVG objects don’t lose any quality as users zoom in or out. You can access SVG objects via the DOM, and—similar to HTML elements—SVG elements support attributes, styles, and event handlers. The element provides a container in which to render graphics; SVG renders inline with the page’s layout. Here’s an example of an SVG graphic with event handlers: Test Web Page function Red(evt) { var circle = evt.target; circle.setAttribute("style", "fill: red"); } function Green(evt) { var circle = evt.target; circle.setAttribute("style", "fill: green"); }



Objective 1.2: Write code that interacts with UI controls

CHAPTER 1

55



This code produces the output shown in Figure 1-41. The JavaScript event handlers turn the circle red when the mouse hovers over it and back to green when the mouse is moved out of the circle.

FIGURE 1-41  A circle drawn using SVG

All the shape-drawing and line-drawing functionality you saw in the element discussion exists for SVG as well, although the syntax is different, of course. The following code produces a slightly more elaborate graphic.

The image in Figure 1-42 shows the output of this code:

FIGURE 1-42  Multiple shapes drawn using SVG

56

CHAPTER 1

Implement and manipulate document structures and objects

In this example, the element was used to create the background rectangle, and a series of elements were used to create the lights. Each SVG shape requires the same types of parameters as their canvas counterpart, and the same rules apply. The element needs an (x,y) coordinate to establish where it should be drawn, along with a width and height to establish the size. The same is true for each circle, except that you specify the radius for the size. The fill attribute sets the color to be used to fill the shape. SVG also supports the same basic shape-drawing functions as the canvas context. The following code segment shows the use of the polyline, polygon, line, and ellipse and produces the output shown in Figure 1-43: Examples of SVG Shapes and Text

FIGURE 1-43  Text, a line, a polygon, an ellipse, and a polyline drawn in different colors

EXAM TIP

In some cases, using SVG graphics is simpler than using the element. As the examples have shown, you can create SVG images declaratively directly within the HTML itself. However, as you increase the number of objects in an SVG rendering, performance can become a concern. In cases where performance is a consideration, using the element is a preferable approach.



Objective 1.2: Write code that interacts with UI controls

CHAPTER 1

57

SVG also supports rendering existing graphics in the form of external image files, as shown here:

Thought experiment Creating a game In this thought experiment, apply what you’ve learned about this objective. You can find answers to these questions in the “Answers” section at the end of this chapter. You were hired to create a game. For the first stage of the game, you must make a ball move from the left side of the screen to right side. How can you achieve this with the HTML5 element? How about with SVG? You demonstrate this stage to stakeholders, and they love it. For the next stage, when the ball is clicked, it must split into two balls. As the number of balls increases due to being clicked, what considerations need to be taken into account? Would using SVG or the element be the better solution for this game?

Objective summary ■■

■■

■■ ■■

■■

58

JavaScript is a powerful tool that enables developers to manipulate the DOM programmatically in the browser. HTML5 supports rich media controls to incorporate video by using the element and audio by using the element. The element supports multiple media formats by using the element. The HTML5 and elements support a rich API to create both simple and complex graphics in the browser. Both the and graphics engines can draw text, lines, shapes, fonts, fills, and gradients.

■■

The element is drawn on via JavaScript by getting a reference to the context.

■■

The element renders graphics by using a declarative syntax.

CHAPTER 1

Implement and manipulate document structures and objects

Objective review Answer the following questions to test your knowledge of the information in this objective. You can find the answers to these questions and explanations of why each answer choice is correct or incorrect in the “Answers” section at the end of this chapter. 1. Which of the following JavaScript methods can’t be used to select an element in the

DOM? A. getElementById B. querySelector C. getElementByClassName D. queryAll 2. Which line of JavaScript successfully retrieves only the image element with the ID

myDog from the following HTML? Choose all that apply. This is a web page about dogs. Here is my dog picture: Here is a picture of my friend’s dog:

A. document.getElementbyId(“myDog”); B. .getChildNode(“img”); C. document.getElementbyId(“dogs).querySelector (“thumb”); D. document.querySelectorAll(“thumb”); 3. To hide an element in the DOM and still be able to add it back later, what should you

do? A. Nothing, because the DOM is always available in a static form. B. Keep a reference to the removed node to be able to add it back. C. Call the document.restoreNodes method. D. You can’t add an element back after it’s removed.



Objective 1.2: Write code that interacts with UI controls

CHAPTER 1

59

4. When implementing the HTML5 video element, how do you ensure that the rendering

of the element can function in different browsers? A. You need to do nothing, because HTML5 is now a standard specification. B. Specify all the source video types in the src attribute of the video element. C. Include the element for each video type so that each browser can play

the version that it supports. D. Include the element for each video type so that the browser can play the

version that it supports. 5. When drawing on the HTML5 element, what method is used on the context

to begin drawing at a new point? A. moveTo B. lineAt C. beginPath D. stroke 6. When performance is critical for an HTML5 graphics application, what should you use? A. using a declarative syntax to create the graphics B. using a declarative syntax to create the graphics C. using JavaScript to create the graphics D. and combination to leverage the best performance of both

Objective 1.3: Apply styling to HTML elements programmatically The section covers applying styles to the HTML elements on the page dynamically, using JavaScript. When you retrieve element references by using methods such as getElementById, you can then manipulate those elements, including their styles.

This objective covers how to:

60

■■

Change the location of an element

■■

Apply a transform

■■

Show and hide elements

CHAPTER 1

Implement and manipulate document structures and objects

Changing the location of an element By using the methods to retrieve an element from the DOM in JavaScript, you can apply styles dynamically through code that can change the element’s position on the page. How elements are laid out on the page can affect how elements behave when they are repositioned. A few options determine how HTML elements are positioned on a webpage. By default, all HTML elements flow statically from left to right in the same order that they are declared in the HTML page. However, CSS provides a mechanism to specify some advanced options in element position. You can position elements by using absolute positioning or relative positioning. With absolute positioning, the element is placed in the exact location specified, relative to its container’s borders. However, with relative positioning, the element is positioned relative to its immediate left sibling’s coordinates. You can apply four properties individually or in combination to control the position of an element: Top, Left, Right, and Bottom. Each property takes a distance parameter that specifies the relative distance of the object from a reference point based on the positioning attribute specified. When using absolute or relative positioning, the default border or margin settings are ignored because the object is positioned where the positioning attributes direct the element to be. The code in Listing 1-3 demonstrates this. LISTING 1-3  HTML and JavaScript to illustrate positioning html, body { height: 100%; width: 100%; } img { height: 150px; width: 225px; } window.onload = function () { var top = document.getElementById("topText"); var left = document.getElementById("leftText"); var pos = document.getElementById("positioning"); document.getElementById("btnPosition").onclick = function () { var img = document.getElementById("orange2"); img.style.position = pos.value; img.style.left = left.value + "px"; img.style.top = top.value + "px"; } }



Objective 1.3: Apply styling to HTML elements programmatically

CHAPTER 1

61

Left: Top: Position: relative absolute

When the code is rendered in the browser, the default position is in effect, as shown in Figure 1-44.

FIGURE 1-44  The default position of two images

All positioning attributes that have been discussed are available declaratively in the style attribute of the HTML element but can also be accessed programmatically and manipulated via JavaScript. The webpage is enhanced to provide some end-user functionality to control the positioning of the two images. The code in Listing 1-3 provides an HTML page with the two images and some input controls to control the positioning of the second image. You can enter the top and left positions as well as whether to position relative to the first orange picture or to position as absolute to the parent table element.

62

CHAPTER 1

Implement and manipulate document structures and objects

When top and left are set to 50px and positioning is relative, you’ll see the result shown in Figure 1-45.

FIGURE 1-45  Positioning the second image relative to its neighboring element

Keeping the values the same but changing the positioning to absolute changes the positioning of the elements, as shown in Figure 1-46.

FIGURE 1-46  Positioning the second image absolute to its parent element.

You can employ yet another mechanism to change the appearance of an HTML element: transforms, which you examine next.

Applying a transform Applying transforms is a way to change an object on the webpage. Transforms enable you to change an element’s appearance. You can make an element larger or smaller, rotate it, and so on. Quite a few transform methods are available. To add a transform to an element, you declare it in the CSS for the element by adding the transform property as follows: .rota {transform: rotate(90deg);}



Objective 1.3: Apply styling to HTML elements programmatically

CHAPTER 1

63

This code applies the rotate method to an object when you add the .rota CSS class to the object’s styles collection. As mentioned, various transform methods are available, and you’ll examine each in turn. Use the following code for all the examples in this section: #orange1 { height: 150px; width: 225px; } .trans { transform: scale(1) ; } window.onload = function () { document.getElementById("orange1").onclick = function () { this.classList.add("trans"); } }

This code creates a single image object to which you’ll apply the transformations; however, the transformations can work successfully against any HTML element. The image also is assigned an event handler for the click event. This suffices for demonstration purposes. You can use any supported event to trigger a transformation. In the examples that follow, you’ll need to replace the .trans CSS class in the preceding code with the appropriate transform methods to demonstrate them. You’ll be prompted to replace the code when needed.

Using the rotate method The rotate transform method enables you to rotate an object by a specified number of degrees. The method accepts a single parameter that specifies the number of degrees. In the previous code used for the scale transformation, replace the transform method with the following: transform: rotate(90deg);

Now, run the webpage in the browser. Click the image to see the transform take effect. In this case, the image is rotated clockwise by 90 degrees (see Figure 1-47). If you instead want to rotate the image counterclockwise, you can specify a negative number of degrees.

64

CHAPTER 1

Implement and manipulate document structures and objects

FIGURE 1-47  The effect of the rotate transformation on an image

The transform also supports the rotateX and rotateY methods, which accept a single parameter in degrees to specify an angle around the x-axis or y-axis in which to rotate. You can, for example, use these methods to flip an element vertically or horizontally by specifying 180deg as the parameter. In this case, the element rotates 180 degrees along the specified axis—which essentially results in the image being flipped or mirrored along that axis.

Using the translate method The translate method lets you move an HTML element by changing its relative X and Y position on the page. You implement the translate method by specifying the translate method on the transform property. In the example listing, replace the transform method with the following: transform: translate(50px,0px);

The translate method moves the HTML element to which it’s applied by 50 pixels in the X direction and 0 pixels in the Y direction relative to where it now resides (see Figure 1-48). Again, translateX and translateY methods are available if the desired effect is to move the object around the x-axis or y-axis.

FIGURE 1-48  The effect of the translate method applied to an image



Objective 1.3: Apply styling to HTML elements programmatically

CHAPTER 1

65

Using the skew method You can skew an HTML element using the skew method of the transform property. Skewing slants the object so that it’s not parallel to the vertical or horizontal axis. In the example code, replace the transform property with the following code line. Figure 1-49 shows the effect transform: skew(10deg, 10deg);

FIGURE 1-49  The effect of the skew method on an image

Using the scale method The scale method enables you to resize elements by a specified ratio. The scale method takes one parameter: a decimal value that represents the percentage to scale. Specifying a value greater than 1 makes the object larger; specifying a value less than 1 but greater than 0 makes the object smaller. Specifying a value of –1 flips the object over its horizontal axis. In the sample code, replace the transform property with the following: transform: scale(1.5);

This scale transform increases the size of the element by 50 percent, essentially multiplying the existing height and width values by 1.5. The object scales out from its absolute center so that it expands in all directions; it doesn’t just extend down and to the right. Figure 1-50 shows the result of a scale transform.

FIGURE 1-50  The effect of the scale transform on an image

66

CHAPTER 1

Implement and manipulate document structures and objects

Combining transformations Transformations individually lend great flexibility to what you can accomplish by changing the appearance of HTML elements, but the transform style isn’t limited to specifying a single transformation method. You can combine the methods to apply multiple effects to the element. In the sample code, change the transform property to the following code: transform: translate(50px,0px) scale(1.5) skew(10deg, 10deg);

In this code, three effects are applied. Order matters. The effects are applied in the order that they are specified in the transform property. In this case, the translate property is applied first, and then the translated object is scaled. Finally, the resulting object is skewed. The effect on the HTML element is that it is moved 50 pixels along the x-axis, scaled by 50 percent, and then skewed 10 degrees.

Showing and hiding elements You can show and hide elements declaratively in the HTML markup or programmatically by modifying the object’s CSS properties through JavaScript. You can create the CSS properties that show or hide an element directly in an object’s style property or in a CSS style, and it is added to the element’s style collection. This section’s examples use the code from Listing 1-2, updated as follows: … window.onload = function () { document.getElementById("btnHideAnElement").onclick = function () { if (document.getElementById("innerDiv").style.display == 'inline') { document.getElementById("innerDiv").style.display = 'none'; } else { document.getElementById("innerDiv").style.display = 'inline'; } } } … Show/Hide Element

This code modifies the script block and adds a new button to the bottom of the page. The button is connected to an onclick event after the window finishes loading. In this event, you modify programmatically the visibility of the HTML elements. The innerDiv element defaults to hidden when the page is loaded. When the button is clicked, the code evaluates the state of the display CSS property to determine whether the element is now visible or hidden. Depending on the result, the property is toggled. The



Objective 1.3: Apply styling to HTML elements programmatically

CHAPTER 1

67

display property accepts two possible values. A value of inline tells the browser to show the item, while a value of none means the browser should hide the item. The second property available for controlling element visibility is called visibility. This property accepts four possible values, as outlined in Table 1-12. TABLE 1-12  Values available for the visibility property

Value

Effect

visible

Sets the property to visible to show the element

hidden

Hides the element

collapse

Collapses the element where applicable, such as in a table row

inherit

Inherits the value of the visibility property from the parent

Some of these values have interesting behaviors. When you use the display CSS property and set it to the value of none, the HTML element is hidden. But hiding the element in this way also removes it from the layout. All the surrounding elements realign themselves as though the element was not there at all. When display is set to inline, the element is shown again and all the surrounding elements move out of the way, back to where they were originally. The visibility CSS property behaves slightly differently. Setting the visibility property to hidden hides an element, but the hidden element’s surrounding elements act as though it’s still there. The space that the element occupied is maintained intact, but the element’s content is hidden. When the property is set back to visible, the element reappears exactly where it was, without affecting any surrounding elements. The collapse value, on the other hand, acts more like the display property. If you specify collapse on something such as a table row, the table rows above and below collapse and take over the space that the collapsed row was occupying. When you set the visibility property back to visible, the surrounding elements move out of the way to show the element. This is useful for situations where you want to have content that can be collapsed or displayed one item at a time to preserve space, such as on an FAQ, where the answer to a question is shown when a user clicks the question but then collapsed when the user clicks a different question. EXAM TIP

If you need to preserve the page layout when altering visibility, use the visibility property with the hidden value. If you don’t need to preserve the layout, you can either set the display property to none or set visibility to collapse.

68

CHAPTER 1

Implement and manipulate document structures and objects

Thought experiment Creating a dynamic survey In this thought experiment, apply what you’ve learned about this objective. You can find answers to these questions in the “Answers” section at the end of this chapter. You have been tasked with building a webpage that requires a user to answer a series of questions. However, the questions are dynamic, based on the user’s answer to a previous question. The user should see only relevant questions. Take the following question flow as the rules for this page:



Objective 1.3: Apply styling to HTML elements programmatically

CHAPTER 1

69

And the following HTML5 page: ... A dynamic Questionnaire. Questionnaire Answer the questions in order as they appear. 1. .... 2. .... 3. .... 4. .... 5. .... 6. .... 7. .... 8. .... 9. .... 10. ....

70

CHAPTER 1

Implement and manipulate document structures and objects



Create all the JavaScript necessary to show and hide the required elements, depending on the answer to each question. Assume that the answer to each question is a radio button selection with only Yes/No options.

Objective summary ■■ ■■

■■

You can use CSS to define transformation effects. You can apply transformations via JavaScript to manipulate the DOM with effects such as rotate, skew, scale, and translate. The visibility property provides options to control an element’s visibility within the page.

Objective review Answer the following questions to test your knowledge of the information in this objective. You can find the answers to these questions and explanations of why each answer choice is correct or incorrect in the “Answers” section at the end of this chapter. 1. Absolute positioning positions an object relative to what? A. The top-left corner of the browser window. B. The top-left corner of its parent element. C. Centered inside the window. D. Centered inside its parent element. 2. Which transformation enables you to change the size of an element? A. rotate B. skew C. translate D. scale 3. Which syntax preserves the layout of the page when hiding an element in the DOM? A. display=’hidden’ B. display=’inline’ C. visibility=’none’ D. visibility=’hidden’



Objective 1.3: Apply styling to HTML elements programmatically

CHAPTER 1

71

Objective 1.4: Implement HTML5 APIs JavaScript APIs have provided some new powerful functionality, such as the ability to store more data locally and make that data available to the webpage through the Web Storage API. The AppCache API allows you to take web applications offline. The Geolocation API provides methods to work with global positioning within the application.

This objective covers how to: ■■

Use the storage API

■■

Use the AppCache API

■■

Use the Geolocation API

Using the storage API WebA Storage is a new API for storing webpage data locally. Web Storage, described in this section, replaces the concept of cookies. NOTE  BROWSER SUPPORT

Of course, you must consider your audience’s browser support for HTML5 and Web Storage before you choose to use it exclusively.

Two forms of Web Storage exist: local and session storage. Local storage is persistent; data stored in local storage is available to the webpage even if the user closes the browser completely and then reopens it to your site. Session storage is available only for the duration of the current session, so if the user closes the browser, session storage is automatically cleaned up and is no longer available. The Web Storage API is available as a global object. To access local storage, use the localStorage object; to access session storage, use the sessionStorage object. EXAM TIP

The localStorage and sessionStorage objects provide exactly the same API. All the examples shown in this section work exactly the same with either object. The only difference is the lifetime of the storage. Remember that sessionStorage is cleared when the session is closed, whereas localStorage is still be accessible after a session closes and a new session opens.

Table 1-13 lists the API methods and their usage. Web Storage is implemented as name value pairs and stored as strings. Any data that you can put into a string format can be stored in Web Storage. This isn’t as limiting as it sounds. You’ll see some examples of storing complex objects.

72

CHAPTER 1

Implement and manipulate document structures and objects

TABLE 1-13  Methods available on storage objects

Method

Description

setItem

Adds a key/value pair into storage. If no item with the specified key exists, the item is added; if that key does exist, its value is updated.

getItem

Retrieves data from storage based on a specified key value or index.

clear

Clears all storage that has been saved. Use this method to clear out the storage as needed.

key

Retrieves the key at a specified index. You can use the resultant key to pass as a parameter to one of the other methods that accepts a key.

removeItem

Removes the specified key/value pair from storage.

In addition to the methods described in Table 1-13, the storage objects expose a length property which returns the number of key/value pairs in storage. Use the sample code in Listing 1-4 to explore the Web Storage API. LISTING 1-4  Exploring the Web Storage API section { margin-top: 15px; } window.onload = function () { document.getElementById("btnAdd").onclick = function () { } document.getElementById("btnRemove").onclick = function () { } document.getElementById("btnClear").onclick = function () { } function LoadFromStorage() { } } Key: Value:



Objective 1.4: Implement HTML5 APIs

CHAPTER 1

73

Add To Storage Remove from Storage Clear Storage Current Storage Contents

The code in Listing 1-4 creates text boxes to accept a key and a value, respectively. Buttons let you add items to storage, remove an item, or completely clear the storage. Each capability is implemented in turn. To display the contents of the storage, the page contains a div that shows the contents of the storage appended to it. The LoadFromStorage method is called for each operation to refresh the page with the data available in the storage. All the following examples use local storage, but again, they would work the same way with session storage. If you want to test these examples using session storage, simply replace the localStorage reference with a reference to sessionStorage. You first need to implement the LoadFromStorage method so that when the page loads, you can see any items that have already been placed into storage. Enter the following code into the LoadFromStorage function in the script block: window.onload = function () { LoadFromStorage(); document.getElementById("btnAdd").onclick = function () { … function LoadFromStorage() { var storageDiv = document.getElementById("storage"); var tbl = document.createElement("table"); tbl.id = "storageTable"; if (localStorage.length > 0) { for (var i = 0; i < localStorage.length; i++) { var row = document.createElement("tr"); var key = document.createElement("td"); var val = document.createElement("td"); key.innerText = localStorage.key(i); val.innerText = localStorage.getItem(key.innerText); row.appendChild(key); row.appendChild(val); tbl.appendChild(row); } } else { var row = document.createElement("tr"); var col = document.createElement("td"); col.innerText = "No data in local storage."; row.appendChild(col); tbl.appendChild(row); }

74

CHAPTER 1

Implement and manipulate document structures and objects

if (document.getElementById("storageTable")) { document.getElementById("storageTable").replaceNode(tbl); } else { storageDiv.appendChild(tbl); } }

Notice that this code added a call to the LoadFromStorage method to the top of the window.onload event, so that localStorage is checked after the page loads. The LoadFromStorage method takes any available elements in local storage and displays them in an HTML table. This code takes advantage of the length property to determine whether any local storage values need to be displayed. If not, the page displays a message about no data in local storage. Add the following code to the button onclick events to start manipulating localStorage: document.getElementById("btnAdd").onclick = function () { localStorage.setItem(document.getElementById("toStorageKey").value, document.getElementById("toStorageValue").value); LoadFromStorage(); } document.getElementById("btnRemove").onclick = function () { localStorage.removeItem(document.getElementById("toStorageKey").value); LoadFromStorage(); } document.getElementById("btnClear").onclick = function () { localStorage.clear(); LoadFromStorage(); }

The preceding code implements each button’s onclick event. A user can now add items to local storage and see what’s in storage. The user can continue adding to local storage in this application until the storage is full. Availability of local storage is limited, and the storage available isn’t consistent across browsers. As of this writing, the documentation states that Microsoft Internet Explorer 10 supports up to about 10 MB of storage. However, that could change and may not be the same in other browsers; some now support only 5 MB of storage. Keep this in mind when designing web applications that take advantage of the Web Storage API. Run the preceding example and add the following items to localStorage: ("Red","FF0000"), ("Green","00FF00"), ("Blue","0000FF").

Figure 1-51 shows the output on the screen after adding these items to the local storage.



Objective 1.4: Implement HTML5 APIs

CHAPTER 1

75

FIGURE 1-51  Storing items in and retrieving them from web storage

Now, if you close the browser and then reopen your page, the items are still available in local storage. Try replacing all uses of localStorage with sessionStorage. This time notice that closing the browser automatically clears out any data in the storage. The benefit to using the Web Storage API instead of cookies is that the data resides locally and stays local. The data doesn’t get sent back and forth to and from the server, as is the case with cookies. Data stored in web storage is organized by root domain. The space allotment is available on a per–root domain basis. For example, domains such as localhost or microsoft.com each get their own secure web storage space. As defined by the API, web storage allows storage only of key/value pairs where the key and the value component are stored as a string. If you need to store more complex objects in web storage, you can use a few techniques. For example, add the following code right before the first call to LoadFromStorage in the onload event: var customer = new Object(); customer.firstName = "Rick"; customer.lastName= "Delorme"; customer.shirtSize = "XL"; localStorage.setItem("cart1", JSON.stringify(customer)); LoadFromStorage();

This code creates a custom object to represent a customer browsing the site and sets that customer’s shirt size. This information is to be kept and used locally, so it doesn’t need to be posted to the server. Local storage is a great solution for this. However, to store the custom object in local data, you need a method to convert the custom object to a string that matches the local storage model. This is where JavaScript Object Notification (JSON) can come in handy. You can serialize the object into a JSON string, give it a key, and then store it in web storage. When you run this application now, it shows the customer object represented as a JSON string in Figure 1-52.

76

CHAPTER 1

Implement and manipulate document structures and objects

FIGURE 1-52  Web storage contents

The availability of local web storage can improve both end-user experience and performance of your web applications by saving round trips to the server to retrieve or store temporary data. You must consider the local web storage as temporary. Even when you’re using localStorage as opposed to sessionStorage, you should think of the storage as temporary and design your applications so that they can fall back on default values and behavior if the user purges the web storage. Web storage provides a way to make data available locally and even persist across browser sessions. These techniques work with a live connected website. If you want to make an application available offline, in a disconnected way, you can use the AppCache API, which is covered next.

Using the AppCache API The ability to continue to work with web applications when disconnected from an Internet source has become particularly important in today’s mobile world. This section talks about how to create an application that works when disconnected by using the Application Cache API, also commonly called the AppCache API. The AppCache API makes content and webpages available even when a web application is in offline mode. AppCache stores files in the application cache in the browser. Just as with Web Storage, the amount of data the browser can store locally is limited for offline use. Two components make up the AppCache API: the manifest file and a JavaScript API to support it.

Using AppCache manifest Specifying that a page should be available for offline use is as easy as adding an attribute to the HTML element in the page. Here’s an example: …

The manifest attribute on the html element tells the browser that this webpage needs to be available offline. The value of the manifest attribute points to a manifest file. The name of the file is a convention more than a requirement; you can name the file anything, but the file extension is usually .appcache.



Objective 1.4: Implement HTML5 APIs

CHAPTER 1

77

EXAM TIP

If you really want to change the file extension, you need to configure the web server so that your chosen file extension is returned with a MIME type of text/cache-manifest.

The application cache manifest file must list each and every file and resource required to be stored for offline use. When the browser parses the manifest attribute of the html element, it downloads the manifest and stores it locally. It also ensures that it downloads all the files listed in the manifest so that they are available offline. The manifest file contains three sections: CACHE, NETWORK, and FALLBACK. Each section might appear just once, multiple times in the file, or not at all. Each serves a specific purpose in how application caching functions when dealing with the resources in specific scenarios. A typical manifest file looks like this: CACHE MANIFEST # My Web Application Cache Manifest # v.1.0.0.25 # #Cache Section. All Cached items. CACHE /pages/page1.html /pages/page2.html #Required Network resources NETWORK: login.html #Fallback items. FALLBACK: login.html fallback-login.html

The first line in a manifest file must be CACHE MANIFEST. The manifest file, as with any code file, can have comment lines added to it for additional explanations, as denoted by the # symbol. The CACHE section lists all the resources that must be cached offline. This must include all CSS files, JPG files, video and audio files, and any other resource required for the page to function correctly. If you omit an item from the manifest file, it won’t be cached, which can result in unexpected behavior when the application is run offline. The NETWORK section declares any resources that must be available from the Internet. These items can’t be cached. Anything that the page requires from the Internet, such as embedded third-party elements, must be listed here. If such a resource isn’t listed here, the browser won’t know to check on the Internet for it when in offline mode. When the browser is in offline mode, it doesn’t attempt to go to the Internet for anything unless it’s listed in the NETWORK section. The FALLBACK section enables you to provide fallback instructions to the browser in the event that an item isn’t available in the cache and the browser is in offline mode. In the

78

CHAPTER 1

Implement and manipulate document structures and objects

example file, if login.html isn’t available in the cache, render fallback-login.html. You can use shortcuts in the FALLBACK section to provide more general redirects, such as the following: /resources /resource.jpg

This tells the browser that if the browser is offline and can’t access anything in the resources folder, it should replace any references to items in the resources folder with resource.jpg. Note that resource.jpg is cached because it’s specified in the FALLBACK section. You don’t need to also specify resource.jpg in the CACHE section.

Using the AppCache API As with Web Storage, the application cache is available in JavaScript as a global object. The following code gets a reference to the AppCache global object: var appCache = window.applicationCache;

When you’re using the application cache to make pages available offline, one of the more useful things you can do when the page is loaded is verify its status. You achieve this by evaluating the status property of the AppCache object. The status property could be one of the values listed in Table 1-14. TABLE 1-14  The application cache status property

Status

Description

Uncached

The web application isn’t associated with an application manifest.

Idle

The caching activity is idle, and the most up-to-date copy of the cache is being used.

Checking

The application manifest is being checked for updates.

Downloading

The resources in the application manifest are being downloaded.

UpdateReady

The resources listed in the manifest have been successfully downloaded.

Obsolete

The manifest can no longer be downloaded, so the application cache is being deleted.

After you know the cache status, two methods on the AppCache object can be useful. Table 1-15 lists these. TABLE 1-15  Methods available with the applicationCache object



Method

Description

swapCache

Indicates that the cache be replaced with a newer version.

update

Tells the browser to update the cache if an update is available.

Objective 1.4: Implement HTML5 APIs

CHAPTER 1

79

When the update method is called, an update to the cache is prepared. When that’s ready to download, the status of the application cache changes to UpdateReady. When this is set, a call to the swapCache method tells the application to switch to the most recent cache. EXAM TIP

The call to the update method is asynchronous. Therefore, you must handle the onupdateready event to determine when the update has completed the download process.

In addition to the properties and methods, the AppCache object can raise a series of events that you can handle. The application cache typically operates in the background, and you won’t need these events. However, in some cases handling some of the events and forcing an update can be useful. Table 1-16 lists the available events. TABLE 1-16  Events available from the applicationCache object

Event

Description

onchecking

The browser is checking for an update to the application manifest, or the application is being cached for the first time.

onnoupdate

The application manifest has no update available.

ondownloading

The browser is downloading what it has been told to do per the manifest file.

onprogress

Files are being downloaded to the offline cache. This event fires periodically to report progress.

oncached

The download of the cache has completed.

onupdateready

The resources listed in the manifest have been newly redownloaded, and the swapCache method might be called.

onobsolete

A manifest file is no longer available.

onerror

An error has occurred. This could result from many things. Appropriate logging is necessary to get the information and resolve.

Most of these events might not be used often, if at all. The most common scenario is to handle the onupdateready method and then make a call to the swapCache method, as in this example: window.onload = function () { var appCache = window.applicationCache; appCache.oncached = function (e) { alert("cache successfully downloaded."); }; appCache.onupdateready = function (e) { appCache.swapCache(); }; }

Using the application cache is more about configuration than about coding. However, it’s important that you’re aware the API is available for advanced scenarios where you need more control over the process, or when you need to receive timely information about the process, such as by handling the onprogress event.

80

CHAPTER 1

Implement and manipulate document structures and objects

Using the Geolocation API Location services have become a large part of most people’s lives. From routing and navigation to just finding nearby points of interest or checking into their favorite social community sites, more and more people are using some form of location services. Location services depend on the Global Positioning System (GPS), IP addresses, and other device characteristics. You can take advantage of geolocation in web applications by leveraging browsers that support the Geolocation API. You can get a reference to the Geolocation API from the window.navigator property, as follows: var geoLocator = window.navigator.geolocation;

This code saves a reference to the Geolocation API in a variable to provide shorthand access to the API during future use. A good practice is to ensure that the client’s browser supports the Geolocation API by making sure that the reference is actually present. The Geolocation API supports three key methods that you use to interact with it: getCurrentPosition, watchPosition, and clearWatch.

Using the getCurrentPosition method Here’s an example of using the getCurrentPosition method: getCurrentPosition(positionCallback, [positionErrorCallback], [positionOptions])

You use getCurrentPosition to get exactly what its name indicates—the current position of the user or the device in which the application is running. This method takes one required parameter and two optional parameters. The first parameter is a callback method that the API calls after the current position is determined. The second parameter is optional, but it’s also a callback function called when an error occurs. The callback method you specify here should handle any errors that can occur when trying to get the current position. The last optional parameter is a special object called PositionOptions, which lets you set some special options that control how the getCurrentPosition method behaves. Table 1-17 lists the possible values. TABLE 1-17  Properties available on the PositionOptions object



Property

Description

enableHighAccuracy

This causes the method to be more resource intensive if set to true. The default is false. If true, the getCurrentPosition method tries to get as close as it can to the actual location.

timeout

This specifies a timeout period for how long the getCurrentPosition method can take to complete. This number is measured in milliseconds and defaults to zero. A value of zero represents infinite.

Objective 1.4: Implement HTML5 APIs

CHAPTER 1

81

Property

Description

maximumAge

If this is set, the API is being told to use a cached result if available, rather than make a new call to get the current position. The default is zero, so a new call is always be made. If maximumAge is set to a value and the cache isn’t older than the allowable age, the cached copy is used. This value is measured in milliseconds.

Listing 1-5 shows the getCurrentPosition method in use, with all parameters specified. LISTING 1-5  Using the getCurrentPosition method window.onload = function () { var geoLocator = window.navigator.geolocation; var posOptions = {enableHighAccuracy: true,timeout: 45000}; geoLocator.getCurrentPosition(successPosition, errorPosition, posOptions); } function successPosition(pos) { alert(pos); } function errorPosition(err) { alert(err); } Current Location is:

When the code runs in the browser, some interesting things can happen. First, browser security starts; users are asked whether they want to allow this application to determine their location. In Internet Explorer, the message looks like the image in Figure 1-53.

FIGURE 1-53  The security warning presented by Internet Explorer when accessing the Geolocation API

If the user chooses to allow the application to proceed, everything is great. Otherwise, the method throws an exception. For purposes of demonstrating the code, select Allow For This Site from the drop-down list so that the page can proceed. It might take a few seconds, but the call returns and shows a message box that a position object exists as passed to the success callback method.

82

CHAPTER 1

Implement and manipulate document structures and objects

Both the success and error callback methods receive one parameter from the Geo­­­location API. The success method receives a position object, whereas the error method receives an error object. The position object exposes two properties: coords and timestamp. The timestamp property indicates the time at which the coords were received. The coords property is itself a coordinates object that contains the latitude, longitude, altitude, heading, and speed of the device’s current position and/or relative to the last position acquired. The positionError object contains two properties: one for the code and one for the message. You can use these objects in Listing 1-5 by adding the following fragments: function successPosition(pos) { var sp = document.createElement("p"); sp.innerText = "Latitude: " + pos.coords.latitude + " Longitude: " + pos.coords.longitude; document.getElementById("geoResults").appendChild(sp); } function errorPosition(err) { var sp = document.createElement("p"); sp.innerText = "error: " + err.message; + " code: " + err.code; document.getElementById("geoResults").appendChild(sp); }

Figure 1-54 shows the output from running this code successfully.

FIGURE 1-54  Displaying current location as retrieved by the Geolocation API

Using the watchPosition method The second method available on the geolocation object is the watchPosition method, which provides a built-in mechanism that continuously polls for the current position. Here’s an example of using the method: geoLocator.watchPosition(successCallBack,errorCallback,positionOptions)

The watchPosition method takes the same set of parameters as the getCurrentPosition method but returns a watchPosition object: var watcher = geoLocator.watchPosition...

After running this code, the watcher variable holds a reference to the watchPosition instance being invoked, which can be useful later. The method calls the success callback method every time the Geolocation API detects a new location. The polling continues forever unless it you stop it. This is where the watcher object comes in handy; you can cancel polling by



Objective 1.4: Implement HTML5 APIs

CHAPTER 1

83

calling the clearWatch method. You could call this method in either the success or the error callback—for example, to cancel polling when you have captured enough position information or when you want to pause polling for a period of time: geoLocator.clearWatch(watcher);

Listing 1-6 shows the full solution code for the watchPosition example. LISTING 1-6  Using the Geolocation API to monitor position var watcher; var geoLocator; window.onload = function () { geoLocator = window.navigator.geolocation; var posOptions = {enableHighAccuracy: true,timeout: 45000}; watcher = geoLocator.watchPosition(successPosition, errorPosition, posOptions); } function successPosition(pos) { var sp = document.createElement("p"); sp.innerText = "Latitude: " + pos.coords.latitude + " Longitude: " + pos.coords.longitude; document.getElementById("geoResults").appendChild(sp); geoLocator.clearWatch(watcher); } function errorPosition(err) { var sp = document.createElement("p"); sp.innerText = "error: " + err.message; + " code: " + err.code; document.getElementById("geoResults").appendChild(sp); }

Figure 1-55 shows the output of this code on a mobile device.

FIGURE 1-55  Multiple positions being recorded by the watchPosition method

84

CHAPTER 1

Implement and manipulate document structures and objects

Thought experiment Combining the JavaScript APIs In this thought experiment, apply what you’ve learned about this objective. You can find answers to these questions in the “Answers” section at the end of this chapter. Consider an application such as a running or walking utility that measures the distance traveled. One feature of this application is to play back a completed route to users if they so choose. How would you use the Geolocation API and Web Storage API in combination to save the data points as users travel the route so the application can play them back later? (Assume that you’re interfacing with map software on which to draw the lines.)

Objective summary ■■

The new Web Storage API enables you to store data locally on the client computer.

■■

Web Storage supports both localStorage and sessionStoroage.

■■

Data in Web Storage is stored as name and value pairs.

■■

The AppCache API provides a way to make webpages available when users are offline.

■■

The AppCache manifest defines what’s available offline.

■■

The Geolocation API provides a way to integrate location services into a webpage.

■■

The Geolocation API provides two methods: getPosition and watchPosition.

Objective review Answer the following questions to test your knowledge of the information in this objective. You can find the answers to these questions and explanations of why each answer choice is correct or incorrect in the “Answers” section at the end of this chapter. 1. When using the Web Storage API, where should you store data to ensure that it’s

cleared when the user closes the browser? A. localStorage B. cookieStorage C. sessionStorage D. A hidden input element



Objective 1.4: Implement HTML5 APIs

CHAPTER 1

85

2. What do you need to do to designate a page as available offline? A. Specify in JavaScript as document.offLine=true. B. Specify the manifest attribute on the form element. C. Specify the manifest attribute on the HTML element. D. Tell users to switch to offline mode using their browser. No code is required. 3. Which of the following aren’t valid sections of the AppCache manifest? A. Cache manifest B. Session manifest C. Network manifest D. Fallback manifest 4. Which event is fired by the AppCache object when the cache download is complete? A. oncached B. onupdateready C. ondownloading D. onchecking 5. When using the Geolocation API, how do you configure the ability to use cached data? A. Set the enableCache property to true on the PositionOptions object. B. Set the maximumAge property to a non-zero value on the PositionOptions object. C. Set the timeout property of the PositionOptions object. D. Using the cache is always on to save bandwidth, so no configuration is required.

Objective 1.5: Establish the scope of objects and variables A key component of any programming language is how it uses variables, and JavaScript is no exception. To use variables effectively, you must understand their scope and lifetime. Declaring variables and instantiating objects consumes resources. The primary system resource used for variables is memory. The more memory an application uses, the greater the potential that usage of other resources will increase also—such as battery power to support the additional memory use. When applications drain the battery, users are less likely to use those applications.

86

CHAPTER 1

Implement and manipulate document structures and objects

This objective covers how to: ■■

Establish the lifetime of variables and variable scope

■■

Avoid using the global namespace

■■

Leverage the this keyword

Establishing the lifetime of variables and variable scope Variables begin their life with a declaration. The remainder of their life within the application depends on where the variables are declared. To declare a variable in JavaScript, you use the var keyword. var myVariable;

You can declare many variables concurrently. For example, the following code declares three variables: var x, y, z;

You can also initialize your variables inline with the declaration, giving them immediate nondefault values: var x =0.0, y=0.0, z=0.0

Until a variable is initialized, it’s not really “alive”—it has a value of undefined. After a variable is available for use, it’s considered to be “in scope.” The duration over which the variable remains in scope depends on where the variable is declared. A variable that has global scope is available throughout the webpage. A variable with local scope is available only within a specified context, as Listing 1-7 shows. LISTING 1-7  An example to demonstrate variable scope div { width: 100px; height: 100px; border: 1px solid black; } var globalVar = "global"; window.onload = function () { var localVar = "local"; document.getElementById("Div1").onclick = function () { var insideDiv1Click; //Do some logic here...



Objective 1.5: Establish the scope of objects and variables

CHAPTER 1

87

}; document.getElementById("Div2").onclick = function () { }; document.getElementById("Div3").onclick = function () { }; function AFunction() { var x; } function BFunctionWithParam(p) { } }

In Listing 1-7, notice that the block creates a section of script available to the entire page. The first line in the script section is the variable globalVar, which is considered global to the entire page. Any JavaScript anywhere on this page could access this variable. At the next level, the code implements a window.onload event handler. Inside this event handler, the first line declares a variable called localVar, which is local to the onload event handler. Inside the onload event handler, the code has access to the globalVar variable. Now things start to get interesting. The onload event handler accesses the DOM to wire up some other event handlers. Inside these event handlers, the code has access to both localVar and globalVar variables. The localVar variable is local to the onload event, but because the other event handlers are declared within the onload event handler, they also have access to local variables declared in the onload event handler. Update the Div1 onclick handler to this code: document.getElementById("Div1").onclick = function () { var insideDiv1Click = "insideDiv1"; alert(globalvar); alert(localVar); alert(insideDiv1Click); };

When this code runs and a user clicks the Div1 element, all three alerts successfully display the value of each variable, which means they are all in scope. Now, update the Div2 onclick handler with this code, the same as was placed into Div1: document.getElementById("Div2").onclick = function () { alert(globalVar); alert(localVar); alert(insideDiv1Click); };

88

CHAPTER 1

Implement and manipulate document structures and objects

When you run this code and click Div2, the globalVar variable is in scope, the localVar variable is in scope, but the insideDiv1Click variable isn’t in scope. That variable lives only within the Div1 onclick handler, so it’s in scope only while that function runs. This example raises an undefined exception when it tries to access the insideDiv1Click variable. As a final example, update the code for Div3 and other functions as follows: document.getElementById("Div3").onclick = function () { var insideDiv3 = "Div3"; AFunction(); BFunctionWithParam(insideDiv3); }; function AFunction() { var x; alert(insideDiv3); } function BFunctionWithParam(p) { alert(p); alert(localVar); }

In this code, the variable insideDiv3 is local to the onclick event handler for Div3. The onclick event handler has access to the globalVar and localVar variables just like the other event handlers did. The event handler for Div3 also calls the AFunction and BFunctionWithParam methods. The AFunction method attempts to access the insideDiv3 variable. Unfortunately, that variable lives only within the scope of the Div3 onclick handler. The functions called from the Div3 click event function don’t inherit or have access to the local variables of the Div3 method. To access the local variables declared in the Div3 event handler from another function, you need to pass them as parameters to those functions. You can also see an illustration of passing a local variable as a parameter to another function in the code. After the call to the AFunction method, the event handler calls the BFunctionWithParam method. This function expects a single parameter named p. The onclick event handler passes the value of the insideDiv3 variable to the method. Now, the p variable is a local variable to the BFunctionWithParam method, so it can show the value of the insideDiv3 variable. This is the only way to make a local variable from one function accessible to another function—by passing a parameter. Next, the BFunctionWithParam method attempts to access the localVar variable. It assumes it would have access, but it doesn’t for the same reason the AFunction method doesn’t have access to the insideDiv3 variable. The localVar variable is accessible only to code within the onload event handler in which it’s declared. For functions outside that scope to have access to it, you need to pass it as a parameter. One more thing to consider with respect to the lifetime and scope of variables is hierarchy. If you plan to use the values of globalVar or localVar variables in the onclick event handlers for the various div elements, you must not declare any variables at the local level with the same name. Locally scoped variables override higher-level scoped variables of the same



Objective 1.5: Establish the scope of objects and variables

CHAPTER 1

89

name. Note that they don’t overwrite them, they override them—meaning that you can’t access the overridden values. The following code demonstrates this point: window.onload … var scaleX = 0.0; … document.getElementById("Div4").onclick = function () { var scaleX = -3; alert(scaleX); } function scaleDiv() { //code to scale the Div by a factor of scaleX }

In this code, if your intention is to use the scaleX variable declared globally within the scaleDiv function, the results should be unexpected. That’s because the function assigned to the onclick event handler also declares a variable named scaleX. The value in the alert window within the onClick function is –3, not 0.0, and when the scaleDiv function accesses the scaleX variable, the value is 0.0. Scoping problems such as these highlight why you must always provide meaningful names to variables. Meaningful names can help prevent accidentally naming variables the same thing.

Avoiding using the global namespace The global namespace is where all the native JavaScript libraries live. In Internet Explorer, the window object references the global namespace. Everything this object exposes is global. The global namespace has far more functionality than this book can cover; however, you’ve already seen some examples of the objects in the global namespace used in this chapter, including the Web Storage API and the Geolocation API. The global namespace includes other nested namespaces, such as Math, WebSocket, and JSON. The global namespace is available to all code within an application session. With the increasing number of third-party libraries in use, and as applications become more complex and require the use of such libraries, the potential for naming conflicts increases. Names of classes within a namespace must be unique. If multiple developers define a namespace with the same name, the JavaScript runtime can’t identify which namespace they intended to use. This is why keeping your objects out of the global namespace is important. One strategy to avoid name collisions is to create your own namespaces for your JavaScript libraries. One pattern to consider using to create unique namespace names is the name of the domain in reverse, such as com.microsoft. Because domain names are unique, this pattern

90

CHAPTER 1

Implement and manipulate document structures and objects

helps reduce the possibility of naming collisions. The following code demonstrates this strategy to create a namespace for a library developed for a bookstore: var com = {}; com.Bookstore = {}; com.Bookstore.Book = { title: 'my book', genre: 'fiction' }; com.Bookstore.Author = { firstName: 'R', lastName: 'D' }

By creating the objects in this way, you can be reasonably certain that if another developer creates a useful library to manage books that you want to include in your site, you won’t have to worry about a naming collision between your Book and Author objects and those provided by the other library. When developing reusable JavaScript libraries, never implement your objects in the global namespace.

Leveraging the this keyword The keyword this is a special term that allows JavaScript developers to reference the containing object directly. The following code snippet demonstrates the context of the this keyword: //Here, "this" references the global namespace this.navigator.geolocation window.onload = function () { //Here, "this" references the window object this... document.getElementById("aDiv").onclick = function() { //Here, "this" references the DIV element this… } }

In this code snippet, the first this reference is in the global namespace—so it provides a direct reference to the global namespace. As you move down through the code, the context of the this keyword changes. In the onload event for the window, this refers to the window object (yes, that’s in the global namespace, but keep reading). Within the onclick function, the this keyword refers to the div element returned from the getElementById method. The this keyword always refers to the object that contains the currently running code. That is its context.



Objective 1.5: Establish the scope of objects and variables

CHAPTER 1

91

Thought experiment Building a custom object library In this thought experiment, apply what you’ve learned about this objective. You can find answers to these questions in the “Answers” section at the end of this chapter. You need to create multiple websites that all leverage the same backend database. You will use AJAX to make server requests for data. You want to create a custom library that can be used among the various websites. You are concerned about naming objects because you know that many other libraries are integrated with the overall solution. How can you design your library so that it doesn’t conflict with other libraries in use?

Objective summary ■■ ■■

■■

■■ ■■

■■

Variables are undefined until they are initialized. Variables are scoped and accessible depending on where they are declared. If they are inside a function, for example, they are local to the function. Passing parameters is the only way to make a local variable available in another function. The global namespace shouldn’t be used because it’s shared by all. You should apply a namespace to custom objects to prevent conflicts in the global namespace. The this keyword provides direct access to the object that raised the event.

Objective review Answer the following questions to test your knowledge of the information in this objective. You can find the answers to these questions and explanations of why each answer choice is correct or incorrect in the “Answers” section at the end of this chapter. 1. In JavaScript, how do you determine the scope of a variable? A. The scope of a variable is global within the context of the page. B. The scope of a variable depends on where inside the script it’s declared. C. The scope of a variable changes depending on the type it represents.

92

CHAPTER 1

Implement and manipulate document structures and objects

2. Why is it important to avoid creating custom JavaScript objects in the global

namespace? A. The global namespace is reserved for the browser. B. The global namespace is available to all applications in the session, and using it

could result in a naming conflict. C. The global namespace creates a security risk to users’ systems. 3. What JavaScript keyword in an event handler can be easily used to reference the object

that raised the event? A. The it keyword provides a reference to the object. B. The document.current property provides a reference to the object. C. The this keyword provides a reference to the object. D. No way is available other than to use a selector query to retrieve the object from

the DOM.

Objective 1.6: Create and implement objects and methods JavaScript is an object-oriented programming language, which means that to develop applications in JavaScript effectively, you must understand how to work with objects. Essentially, two types of objects exist in JavaScript: ■■ ■■

Native JavaScript objects, which are provided with JavaScript itself Custom objects, which developers create to represent their own data constructs and behaviors

In some cases, creating an entirely new object isn’t necessary. You can base objects on other objects (if they are a subtype of that object) by using object inheritance, in which one object inherits all the attributes and behaviors of another object but can also implement additional aspects that are unique to it. Objects encapsulate functionality and state information that is relevant for them. The functionality is provided in the form of methods, whereas state information is provided in the form of properties. This objective examines working with objects in JavaScript.

This objective covers how to: ■■ ■■



Implement native objects Create custom objects and custom properties for native projects using prototypes and functions

■■

Implement inheritance

■■

Implement native methods and create custom methods

Objective 1.6: Create and implement objects and methods

CHAPTER 1

93

Implementing native objects Native objects are available to developers directly through JavaScript. JavaScript provides a large number of objects that provide functionality to make developers’ lives easier. Although covering every native object in JavaScript is out of scope for this book and this exam, you will be expected to be able to create and work with native JavaScript objects. Some native objects are available statically, which means you don’t need to create an instance of them. Others require you to create an instance. You can find both types among objects in the global namespace. One example of a static object is Math, which is available in the global namespace and provides a great deal of functionality without you having to create an instance: var squareValue = Math.sqrt(144);

Other objects, such as Array shown in the following code, require you to create an instance to work with them: var listofPrimeNumbers = new Array(1, 2, 3, 5, 7, 11, 13, 17, 19, 23);

This code introduces the new keyword, which you use to instantiate an object. This tells the runtime to allocate a new object of the type specified. In this case, a new Array object is being requested. The list after the Array object type is called the object’s constructor. This information can be passed into the object as parameters to construct the initial state of the object. Some objects have many constructors to choose from, with differing sets of parameters. The addition of multiple constructors is called an overloaded constructor. JavaScript also provides wrapper objects. These wrap up native types, for example. Native types are defined as integer, string, char, and so on. When a variable is declared like this, var txt = "my long string"; var num = 5;

you can access method on the variable like this: var index = txt.indexOf("long",0); var exp = num.toExponential(5);

The underlying types for string and integer don’t natively have methods or functionality; however, the JavaScript runtime creates a wrapper object for them dynamically so that some useful methods are available. For example, you could create the following string and number variables with the new keyword, but that’s not very common. var txt = new String("my long string"); var num = new Number(5);

The syntax reviewed thus far applies to both native objects and custom objects. Custom objects are created by the developer, whereas native objects are provided by core JavaScript.

94

CHAPTER 1

Implement and manipulate document structures and objects

Creating custom objects Creating custom objects is standard practice when working with information in custom applications. Because JavaScript is an object-oriented language, you should apply proper objectoriented practices when developing JavaScript applications. In almost all cases, this involves creating custom objects to encapsulate functionality within logical entities. For example, the following code creates a book object. This is a dynamic object, meaning that it’s created inline with a variable declaration. var book = { ISBN: "55555555", Length: 560, genre: "programming", covering: "soft", author: "John Doe", currentPage: 5 }

The object created represents a book. It provides a way to encapsulate into a single object the properties that apply to a book—in this case, a book entity. The code specifies five properties. When using the book variable, you can access all the properties just as with any other property; if desired, you could output to the screen by placing the values into the DOM. The properties of an object represent its state, whereas the methods of an object provide its behavior. At this point, the book object has only properties. To give the book object some behavior, you can add the following code: var book = { ISBN: "55555555", Length: 560, genre: "programming", covering: "soft", author: "John Doe", currentPage: 5, title: "My Big Book of Wonderful Things", flipTo: function flipToAPage(pNum) { this.currentPage = pNum; }, turnPageForward: function turnForward() { this.flipTo(this.currentPage++); }, turnPageBackward: function turnBackward() { this.flipTo(this.currentPage--); } }

In the book object, three methods have been added: turnPageForward, turnPageBackward, and flipTo. Each method provides some functionality to the book object, letting a reader move



Objective 1.6: Create and implement objects and methods

CHAPTER 1

95

through the pages. The interesting parts of this code are the function declarations themselves. For example, when you look at the code for the flipTo function, you might think that the function is called FlipToAPage because that’s what was declared. However, this isn’t the case. The methods are called using the alias property that assigned the function. When using the code, the runtime knows that it’s a method, not a property, and it expects the method to be called with parentheses: //This line throws an exception because the object does not support this method book.FlipToAPage(15); //This line works because this is what the method has been named. book.flipTo(15);

Creating objects inline as the book object is in the previous code sample is useful only when it is used in the page where it’s defined, and perhaps only a few times. However, if you plan to use an object often, consider creating a prototype for it so that you can construct one whenever you need it. A prototype provides a definition of the object so that you can construct the object using the new keyword. When an object can be constructed, such as with the new keyword, the constructor can take parameters to initialize the state of the object, and the object itself can internally take extra steps as needed to initialize itself. The following code creates a prototype for the book object: function Book() { this.ISBN = "55555555"; this.Length = 560; this.genre= "programming"; this.covering = "soft"; this.author = "John Doe"; this.currentPage = 5, this.flipTo = function FlipToAPage(pNum) { this.currentPage = pNum; }, this.turnPageForward = function turnForward() { this.flipTo(this.currentPage++); }, this.turnPageBackward = function turnBackward() { this.flipTo(this.currentPage--); } } var books = new Array(new Book(), new Book(), new Book()); books[0].Length …

EXAM TIP

JavaScript consists of objects. Everything in JavaScript is an object. Each object is based on a prototype. Whenever you create a new instance of an object, that instance is based on the object’s prototype.

96

CHAPTER 1

Implement and manipulate document structures and objects

In the preceding code, the Book object is constructed so that you can create one with default properties set. Then, the code creates an Array containing a list of books. You can access each element of the array to initialize each Book object as it’s needed. Accessing each Book element to provide initialization values isn’t terribly efficient. It would be more convenient if the Book object supported more than one constructor. That way, you could create a blank book or create one with specific unique properties. This is where prototyping comes in handy. The following code creates a prototype containing two constructors that support the needs of any users of the Book object: function Book() { //just creates an }

empty book.

function Book(title, length, author) { this.title = title; this.Length = length; this.author = author; } Book.prototype = { ISBN: "", Length: -1, genre: "", covering: "", author: "", currentPage: 0, title: "", flipTo: function FlipToAPage(pNum) { this.currentPage = pNum; }, turnPageForward: function turnForward() { this.flipTo(this.currentPage++); }, turnPageBackward: function turnBackward() { this.flipTo(this.currentPage--); } }; var books = new Array(new Book(), new Book("First Edition",350,"Random"));

With this new code, you can create an empty Book object by using the constructor with no parameters, or you can create a Book object by using specific parameters to initialize some fields.



Objective 1.6: Create and implement objects and methods

CHAPTER 1

97

Objects can contain other objects as needed. In this example, the Author property could easily be factored into a new prototype, making it more extensible and encapsulating the information related to an author. Add the following code to the Book prototype: Book.prototype = { ISBN: "", Length: -1, genre: "", covering: "", author: new Author(), currentPage: 0, title: "", … } function Author(){ } function Author(firstName, lastName, gender) { this.firstName = firstName; this.lastName = lastName; this.gender = gender; } Author.prototype = { firstName:"", lastName:"", gender:"", BookCount: 0 } var books = new Array(new Book(), new Book("First Edition",350, new Author("Random","Author","M")) );

Now, the book’s Author is a custom object instead of just a string. This provides for more extensibility in the design. If you later decide that you need to add information about the author, you can simply add the property or properties to the Author prototype. EXAM TIP

You can add properties to a prototype dynamically rather than use the preceding method. The following code achieves the same outcome. Using such code is just a matter of preference. Book.prototype.ISBN = ""; Book.prototype.Length = 350; Book.prototype.genre = ""; Book.prototype.covering = ""; Book.prototype.author = new Author(); Book.prototype.currentPage = 0; Book.prototype.title = "";

98

CHAPTER 1

Implement and manipulate document structures and objects

Implementing inheritance In object-oriented programming, inheritance is a fundamental concept. In standard objectoriented programming, classes are created in a relational hierarchy, so that the attributes and functionality of one entity can be reused within another entity without having to re-create all the code. In object-oriented parlance, if an entity satisfies the “is-a” relationship question, it’s a candidate for inheritance. For example, an organization is made up of employees, in which an employee entity has certain attributes (properties) and behaviors (methods). Management, executives, and staffers are all types of employees. A staffer “is-a” employee. So in an objectoriented design, a staffer object would inherit from an employee. This type of inheritance is quite easy to build in full-fledged object-oriented languages. However, JavaScript is a special situation because it doesn’t use classes. As you saw in the previous sections, everything is an object; a custom object is made up of properties where some properties are native types and some properties are assigned to functions to implement methods. This section examines object inheritance as it works in JavaScript. Building on the code used in the previous section, this section explains object inheritance. In the preceding code sample, you created an object called Book. But many types of books exist. To extend the definition of Book, you must separate the differences in functionality between, for example, pop-up books and other books. Pop-up books have some extra functionality, such as displaying the pop-up on the current page and perhaps playing a sound. In other words, while a pop-up book “is-a” type of book, it also has this extra functionality that doesn’t apply to all books. In this case, it would be useful to inherit from Book so that all the basic attributes and behaviors of a book are available without you having to re-create them. Then you could add the specific functionality for pop-up books. You can extend the Book object in a couple of ways. (Extending is another way of thinking about inheritance—an object is extended into another object.) Here’s the first way to extend an object: var popupBook = Object.create(Book.protoType,{ hasSound: {value:true}, showPopUp:{ value: function showPop() { //do logic to show a popup } } });

Object.create is a method available from the Object class in the global namespace. The create method takes two parameters: the object you want to create and a list of property descriptors. The first parameter expects to receive the prototype of the object to create or null. If null is specified, the object uses only those functions or properties specified in the second parameter. If an object prototype is specified, as in the case Book.prototype, the object is created with all the properties and functions declared on that object prototype. This is another reason designing code in a proper object-oriented way is important—so that you can leverage this type of functionality to keep code more readable and maintainable.



Objective 1.6: Create and implement objects and methods

CHAPTER 1

99

The second parameter enables you to add properties or behaviors to the object being created. Essentially, you define this additional prototype information inline with the object creation. This example adds the property hasSound, which has a default value specified as false. You could also specify additional information here, such as whether the property is read-only and whether it’s enumerable. Creating objects this way is similar to the inline example in the beginning of the earlier section on custom objects. Again, such an approach isn’t very modular or reusable. For every instance of a pop-up book, you’d need to declare the additional property and method. So again, for objects that you might want to reuse often, extending the Book prototype is better. Extending the Book prototype is much the same as creating a new prototype. You need only one line of code to tell JavaScript to inherit the functionality and attributes of another object. You do this by initializing the prototype to the parent object: function PopUpBook() { Book.call(this); } PopUpBook.prototype = Book.prototype; PopUpBook.prototype.hasSound = false; PopUpBook.prototype.showPopUp = function ShowPop() { };

In this way, PopUpBook now extends the implementation of the Book object and adds its own functionality for reuse. The function PopUpBook makes a method call to Book.call(..). This is a call to the constructor of the super class (the class being inherited from). If the super class has a constructor that takes parameters, this method would enable you to pass the parameter values to the super-class constructors for object initialization.

Thought experiment Creating synergy between custom objects and native objects In this thought experiment, apply what you’ve learned about this objective. You can find answers to these questions in the “Answers” section at the end of this chapter. In this objective you’ve seen the use of native objects and custom objects. How would you bring those two worlds together? In JavaScript, inheriting from native objects is not fully supported. However, what if you wanted to add functionality to native objects by extending them? How would you go about doing this?

100

CHAPTER 1

Implement and manipulate document structures and objects

Objective summary ■■

Everything in JavaScript is an object—even functions.

■■

JavaScript supports native objects and custom objects.

■■

Objects are created with the new keyword.

■■

■■ ■■

■■

Access methods and properties on objects with the dot notation: object.method or object.property. You can create custom objects dynamically or by using prototypes. Prototypes provide for object definition reuse, whereas dynamic objects require attributes and methods defined for each use. Inheritance is achieved in JavaScript through the extension of prototypes.

Objective review Answer the following questions to test your knowledge of the information in this objective. You can find the answers to these questions and explanations of why each answer choice is correct or incorrect in the “Answers” section at the end of this chapter. 1. In JavaScript, which of the following isn’t a native object? A. Function B. Array C. Integer D. Person 2. Which of the following snippets shows the correct way to create a custom book object? A.

var book = “Title: ‘My book about things’” + “Author: ‘Jane Doe’” + “ Pages: 400”;

B.

var book = {Title: “My book about things”, Author: “Jane Doe”, Pages: 400};

C.

var book = (Title= “My book about things”, Author= “Jane Doe”= Pages: 400);

D.

var book = new {Title: “My book about things”, Author: “Jane Doe”, Pages: 400};

3. Inheritance is accomplished in JavaScript through the use of which construct? A. inherits keyword B. implements keyword C. this keyword D. Prototypes



Objective 1.6: Create and implement objects and methods

CHAPTER 1

101

Answers This section contains the solutions to the thought experiments and answers to the objective review questions in this chapter.

Objective 1.1: Thought experiment The following HTML shows the conversion of the page to HTML5: A Thoughtful Experiment Home Page 1 Page 2 Page 3 An Article regarding thought is presented here. Thought as a provoking element. ..................... Here are some reference materials. ..... This page is copyright protected.

102

CHAPTER 1

Implement and manipulate document structures and objects



Objective 1.1: Review 1. Correct answer: D A. Incorrect: The element is a new HTML5 semantic element. B. Incorrect: The element is a new HTML5 semantic element. C. Incorrect: The element is a new HTML5 semantic element. D. Correct: The element isn’t new in HTML5. However, new input types have

been introduced to the specification. 2. Correct answer: A A. Correct: The element is expected to contain any or all of the to

elements. B. Incorrect: The element is used separately to define the header section

of an element or a element. C. Incorrect: The element is used to define a menu structure. It wouldn’t be

included inside an element. D. Incorrect: The element is expected to contain only the to

elements. 3. Correct answer: C A. Incorrect: The element doesn’t provide any additional context to a search

engine. B. Incorrect: The element is contained inside an element and will

help but isn’t the main element to provide search engine optimization. C. Correct: The element tells the search engine that specific content in this

area is relevant to what users are searching for. This element provides for the best search engine optimization. D. Incorrect: The element is specifically used for SEO.

Answers

CHAPTER 1

103

4. Correct answer: C A. Incorrect: elements flow left to right and top to bottom and don’t provide

any structure. B. Incorrect: The element denotes a paragraph. This element doesn’t provide

any layout structure to the page. C. Correct: You use the element to provide a structured layout, using its

rows and columns elements as needed to create the desired layout. D. Incorrect: The element is used to denote an area where the user can sub-

mit data. The element doesn’t provide any mechanism to control layout.

Objective 1.2: Thought experiment This thought experiment speaks directly to performance. As the number of balls increases, the demand on the graphics engine and local resources such as CPU and memory becomes more intense. The element is better designed to perform this duty. From a coding perspective, this experiment provides insight into capturing events from the canvas, calculating the location of the click in relation to the balls on the screen, and creating new balls dynamically as they get clicked. These computations need to occur quickly as the graphics are redrawn. The implementation of such a game would be too involved for the scope of this book. However, the notion of performance is an important concept to be clear on with respect to graphics rendering.

Objective 1.2: Review 1. Correct answer: D A. Incorrect: The getElementById method retrieves an element by its unique id. B. Incorrect: The querySelector method retrieves a single element that matches the

specified selector. C. Incorrect: The getElementsByClassName method retrieves all the elements that

have the specified CSS class assigned to them. D. Correct: The queryAll method isn’t available to search the DOM. 2. Correct answer: A A. Correct: document.getElementbyId(“myDog”); retrieves only the single image with

the ID myDog. B. Incorrect: .getChildNode(“img”); isn’t a valid syntax. C. Incorrect: document.getElementbyId(“dogs).querySelector (“thumb”); fails

because the page has no element named “dogs”. D. Incorrect: document.querySelector.querySelectorAll(“thumb”); is incorrect

because it returns all the elements on the page with the specified class name. 104

CHAPTER 1

Implement and manipulate document structures and objects

3. Correct answer: B A. Incorrect: The DOM is dynamic and changes physically when items are removed

from it. B. Correct: When an element is removed and needed again, a reference to the

removed node must be kept to be able to add it back. C. Incorrect: No such method exists. D. Incorrect: A node can be added back to the DOM if a reference was kept. This is

accomplished by using the various methods available to insert nodes in the DOM. 4. Correct answer: C A. Incorrect: Different browsers support different media formats. Use the

element to provide the media in the various formats. B. Incorrect: The src attribute allows you to specify only one source video. This

doesn’t work across multiple browsers unless the video format is supported across the browsers. C. Correct: The elements specify multiple video formats so that the

browser can choose the correct one. D. Incorrect: The element is supported to provide a fallback mechanism in

the event that the browser doesn’t support the HTML5 element. 5. Correct answer: C A. Incorrect: The moveTo method moves the current context to a new point but

doesn’t begin a drawing. B. Incorrect: The lineAt method draws a line within the current positional context. C. Correct: The beginPath method tells the context to start a new drawing from its

current point. D. Incorrect: The stroke method tells the context to draw the graphics that are

applied to the context. 6. Correct answer: C A. Incorrect: The element doesn’t support any declarative elements. B. Incorrect: The element has poorer performance than the

element when a lot of graphic refresh is required. C. Correct: The element provides superior performance compared to the

element. D. Incorrect: The and elements combined don’t provide better

performance. The element provides superior performance when the graphics require a lot of refreshing.

Answers

CHAPTER 1

105

Objective 1.3: Thought experiment To complete this thought experiment, you must get a reference to the DOM via JavaScript and set up events for the click to each radio button. When an event is created for notification of the selection of each radio button, the questions can be shown/hidden as needed. Setting the display CSS attribute of the elements so that the questions present in flow is key. The desired effect is for the surrounding elements to behave as though the element is no longer part of the DOM. Setting display=none achieves this effect.

Objective 1.3: Review 1. Correct answer: B A. Incorrect: The element isn’t positioned relative to the browser window. B. Correct: Absolute positioning positions the element relative to its parent element. C. Incorrect: The element doesn’t center within the window. D. Incorrect: Absolute positioning doesn’t center an element. 2. Correct answer: D A. Incorrect: The rotate transform spins an object clockwise or counterclockwise. B. Incorrect: The skew transform slants an object. C. Incorrect: The translate transform moves an object. D. Correct: The scale transform changes the size of an object. 3. Correct answer: D A. Incorrect: Display=’hidden’ isn’t a valid option. B. Incorrect: Display=’inline’ shows an object that previously wasn’t showing. C. Incorrect: Visibility=’none’ isn’t a valid option. D. Correct: Visibility=’hidden’ hides an element, and its surrounding elements remain

in place as though the element was still there.

106

CHAPTER 1

Implement and manipulate document structures and objects

Objective 1.4: Thought experiment This is a simple example of the power available when combining the HTML5 APIs. You can use the Web Storage API to store the location points as they occur from the Geolocation API by using the watchPosition method. The following code demonstrates the storage of the data: var watcher; var geoLocator; var positions = 0; window.onload = function () { geoLocator = window.navigator.geolocation; var posOptions = { enableHighAccuracy: true, timeout: 45000 }; watcher = geoLocator.watchPosition(successPosition, errorPosition, posOptions); } function successPosition(pos) { //on each position, store it into local storage sequentially. //Then it can be retrieved sequentially //in order to redraw the route on a map. var p = "Lat: " + pos.coords.latitude + " Long: " + pos.coords.longitude; localStorage.setItem(position, p); } function errorPosition(err) { var sp = document.createElement("p"); sp.innerText = "error: " + err.message; + " code: " + err.code; document.getElementById("geoResults").appendChild(sp); }

Objective 1.4: Review 1. Correct answer: C A. Incorrect: localStorage is persistent even after the session closes. B. Incorrect: cookieStorage doesn’t exist in the Web Storage API. C. Correct: sessionStorage clears when the session closes. D. Incorrect: A hidden input element isn’t a valid solution to meet the requirement. 2. Correct answer: C A. Incorrect: The offLine property isn’t a valid JavaScript option. B. Incorrect: You wouldn’t specify the manifest on the element. C. Correct: Specifying the manifest attribute on the HTML element is the correct

action. D. Incorrect: The browser’s offline option doesn’t invoke the AppCache API.

Answers

CHAPTER 1

107

3. Correct answer: B A. Incorrect: Cache manifest lists all resources that must be cached offline. B. Correct: Session manifest isn’t a valid option. C. Incorrect: Network manifest specifies any resources that must be available from

the Internet. D. Incorrect: Fallback manifest enables you to tell the browser what to do when

resources aren’t available offline. 4. Correct answer: A A. Correct: The oncached event is fired when the download completes. B. Incorrect: The onupdateready event is fired when items in the manifest are newly

downloaded and the swapCache method can be called. C. Incorrect: The ondownloading event is fired when the browser is downloading. D. Incorrect: The onchecking event is fired when the browser is checking for updates. 5. Correct answer: B A. Incorrect: The enableCache property doesn’t exist. B. Correct: Set the maximumAge property to a non-zero value on the

PositionOptions object. C. Incorrect: The timeout property specifies how long to wait for a response before

firing the timeout event. D. Incorrect: The cache maximumAge defaults to 0, so caching is off by default.

Objective 1.5: Thought experiment In this experiment, you took a look at a real-world scenario where the addition of JavaScript libraries could potentially conflict with other libraries already in use. This is a case for using namespaces to scope the library with a unique fully qualified name to ensure that it’s distinct from any other. Review the objective on the use of the global namespace to ensure that you understand this concept for the exam.

Objective 1.5: Review 1. Correct answer: B A. Incorrect: Variables are global only if declared in the global space. B. Correct: The scope of a variable depends on where inside the script it’s declared. C. Incorrect: A variable’s type doesn’t affect its scope.

108

CHAPTER 1

Implement and manipulate document structures and objects

2. Correct answer: B A. Incorrect: The global namespace isn’t reserved for the browser. B. Correct: Because the global namespace is available to all applications in the

session, using it could result in a naming conflict. C. Incorrect: The global namespace doesn’t create a security risk to the user’s system. 3. Correct answer: C A. Incorrect: The it keyword doesn’t exist. B. Incorrect: The document.current keyword doesn’t provide the reference. C. Correct: The this keyword provides a reference to the object. D. Incorrect: The this keyword provides a direct shortcut to the element that raised

the event.

Objective 1.6: Thought experiment To extend a native object, you must add functionality to its prototype. This isn’t ideal object orientation in all cases; however, it does provide another avenue to achieve the desired results. JavaScript objects are dynamic, meaning that the prototype can be modified to add functionality to them. In this example, you extend the Array object to provide a sum method that returns the sum of all the elements in the array. For this type of example, type safety is a concern. Arrays can hold any type of data. True inheritance would allow you to create a special type of array to hold only numbers. In this code, you can add validation to ensure that the values in the array are numeric or add only the numeric values. However, for the sake of demonstrating the extension of the object, this sample omits that validation. The following code shows how you can extend the Array object to support a sum method by adding a new method to its prototype: Array.prototype.sum = function () { var res = 0; for (var i = 0; i < this.length; i++) res += this[i]; return res; }; var x = new Array(2); x[0] = 5; x[1] = 6; document.write(x.sum());

Answers

CHAPTER 1

109

Objective 1.6: Review 1. Correct answer: D A. Incorrect: Functions are objects in JavaScript. B. Incorrect: Arrays are objects in JavaScript. C. Incorrect: Integers are objects in JavaScript. D. Correct: People aren’t native JavaScript objects. 2. Correct answer: B A. Incorrect: This code segment produces a string. B. Correct: This code segment creates a dynamic object. C. Incorrect: This segment is incorrect because (..) was used instead of {}, along with

= instead of :. D. Incorrect: The new keyword isn’t used when creating a dynamic object. 3. Correct answer: D A. Incorrect: The inherits keyword isn’t a JavaScript construct. B. Incorrect: The implements keyword isn’t a JavaScript construct. C. Incorrect: The this keyword doesn’t provide inheritance. D. Correct: Prototypes are used to create inheritance trees in JavaScript.

110

CHAPTER 1

Implement and manipulate document structures and objects

CHAPTER 2

Implement program flow Being able to manipulate the Document Object Model (DOM), create animations, and use the various application programming interfaces (APIs) provided by the JavaScript library is a great skill to have. To leverage the power of the user experience fully, however, you need to provide users with certain website functions only under certain conditions, a concept known as program flow. Without program flow, JavaScript programs would process from top to bottom in the order in which the code was written. This is useful in some cases, but in most situations in which a dynamic user experience is required, logic needs to be processed conditionally. Program flow can be conditional, iterative, or behavioral: ■■

■■

■■

Conditional program flow is based on evaluating state to make a decision as to which code should run. Iterative flow is the ability to process lists or collections of information systematically and consistently. Behavioral flow can be defined as an event or callback in which specific logic should be applied based on user engagement with the web application or the completion of another task.

Flow can—and almost always will—include a combination of all three. Another special type of program flow involves exception handling. Exception handling constructs provide the ability to run specific logic in the case of an error in the program.

Objectives in this chapter: ■■

Objective 2.1: Implement program flow

■■

Objective 2.2: Raise and handle an event

■■

Objective 2.3: Implement exception handling

■■

Objective 2.4: Implement a callback

■■

Objective 2.5: Create a web worker process

111

Objective 2.1: Implement program flow For the exam, you need to understand both conditional and iterative program flow. Conditional program flow enables an application to examine the state of an object or variable to decide which code path to process. The commands you use to apply the concept of conditional flow are the if…else statement, the switch statement, and the ternary operator.

This objective covers how to: ■■

Evaluate expressions, including using switch statements, if/then statements, and operators

■■

Work with arrays

■■

Implement special types of arrays

■■

Use advanced array methods

■■

Implement iterative control flow

Evaluating expressions To use a conditional flow statement, you must evaluate some data against some condition, which you do by using conditional operators. You can use logical operators to combine conditional operators. Combining operators is useful when more than one condition must be met—or at least one condition from a set of conditions must be met—before processing specific logic. Table 2-1 outlines the available operators. TABLE 2-1  Conditional and logical operators

112

Operator

Type

Description

>

Conditional

Evaluates whether the value on the left is greater than the value on the right

<

Conditional

Evaluates whether the value on the right is greater than the value on the left

>=, or < but with the additional logic that the values can also be equal

!=

Conditional

Evaluates whether the values aren’t equal

==

Conditional

Evaluates whether the values are equal independent of the underlying data type

===

Conditional

Evaluates whether the values are equal both in value and underlying data type

&&

Logical

The AND logical operator, in which the expressions on both sides must evaluate to true

||

Logical

The OR logical operator, in which at least one expression on either side must evaluate to true

CHAPTER 2

Implement program flow

Use these operators to evaluate data and to make decisions. For example, if a website requires that its users be a minimum age to sign up for an account, the logic on the sign-up page might include something like this: if(users age >= minimum age) { //allow sign-up. }

Using if statements Use the if statement to evaluate state to control the direction in which the code will run. The if statement can stand alone, as shown in the preceding snippet, or be combined with else to form more complex constructs: if(exp1, exp2, exp3…expn){ //true logic }else { //false logic }

This if statement starts on a new line with the keyword if. In parentheses following the if statement is a series of one or more expressions, separated by logical operators when more than one expression is provided. The code block immediately following the if statement conditional expression runs only when the expression evaluates to true. When the expression evaluates to false, the block immediately following the else keyword runs. The else keyword is optional. An if statement can exist as a standalone statement when no logic is available to run when the expression evaluates to false. EXAM TIP

Two conditional operators are available for checking equality: == (equality operator) and === (identity operator). Checking for equality with the == operator will ignore the underlying data type, whereas the === identity operator will consider data type. Look at the following example: var n = 2000, s = '2000'; alert(n == s); alert(n === s);

The first expression, which uses the equality operator, evaluates to true because the string is cast to a number for the purpose of the evaluation. The second expression, which uses the identity operator, evaluates to false because the string ‘2000’ isn’t equal to the integer 2000.



Objective 2.1: Implement program flow

CHAPTER 2

113

Conditional statements such as the if statement can be nested, like in the following example: var userAge = 10, gender = 'M'; var minimumAge = 11; if (userAge > minimumAge) { if (gender == 'M') { //do logic for above age male } else { //do logic for above age female. } } else if (gender == 'M') { //do logic for underage male } else { //do logic for underage female. }

In this example, the logic tests whether a user is older than a specified age. If the user is over the specified age, the logic in the true branch runs. At that point, another if statement evaluates the user’s gender. If the user’s age is younger than the required minimum, the false branch is processed. Here an else if statement performs additional conditional processing on the false branch based on the gender. Again, the code processes a specific branch depending on whether the user is male or female. You aren’t limited in how deeply you can nest if statements, but nesting them too deeply can make the code quite messy and difficult to read. The following example examines the background color of an element and processes specific behavior based on the color: var canvas = document.getElementById("canvas1"); if (canvas.style.backgroundColor == 'green') { alert('proceed'); } else if (canvas.style.backgroundColor == 'yellow') { alert('slow down/safely stop'); } else if (canvas.style.backgroundColor == 'red') { alert('stop'); }

This code retrieves a reference to a page element called canvas1 and then evaluates that element’s background color to determine an appropriate action to take. In some places, whether by law or preference, yellow sometimes means “proceed faster.” In that case, the code could be adapted to use the OR logical operator: var canvas = document.getElementById("canvas1"); if (canvas.style.backgroundColor == 'green' || canvas.style.backgroundColor == 'yellow') { alert('proceed'); } else if (canvas.style.backgroundColor == 'red') { alert('stop'); }

114

CHAPTER 2

Implement program flow

This code provides the “proceed” instruction when the color is green OR yellow. EXAM TIP

When using the logical OR operator in an if statement, the JavaScript engine knows that it can proceed if any of the statements are true. As such, it evaluates the expressions from left to right until it finds one that’s true. As soon as it does, it won’t evaluate any further expressions but will immediately jump into the true code block. In the preceding example, if the background is green, the check for whether the background is yellow would never be evaluated.

Structuring code this way is syntactically correct. However, lengthy if statements can prove difficult to read and even harder to maintain. If your if statements are becoming quite long—for example, if the previous code example had to test for 15 different colors—a switch statement might be more appropriate.

Using switch statements The switch statement provides a construct in which you can test a list of values for equality (as with the == operator). The following example demonstrates a switch statement: switch (canvas.style.backgroundColor) { case 'yellow': alert('slow down'); break; case 'green': alert('proceed'); break; case 'red': alert('stop'); break; default: alert('unknown condition'); break; }

The switch statement consists of several parts. The first is the switch keyword itself, followed by parentheses surrounding an expression to evaluate. This particular example evaluates the background color of the canvas element. Following the switch line is a series of case statements enclosed in braces. The case statement provides the values to evaluate against. This example provides three cases to evaluate: one for each of the possible red, green, and yellow background colors. Each case statement contains a required break keyword. This keyword denotes the end of that particular case statement. Only the first case that evaluates to true in a switch statement will be processed. Omitting the break keyword will cause unexpected behavior. The last piece of the switch statement is the optional default keyword, which serves as a failsafe. If none of the case statements evaluate to true, the default statement provides a way to handle the situation. You might not want to take any action when none of the case statements evaluates to true—in which case you can omit the default statement. However, it does

Objective 2.1: Implement program flow

CHAPTER 2

115

enable you to handle the scenario where one of the conditions should have been reached but wasn’t, possibly due to bad data being passed into a method or a valid case being missed. Including a default to account for both of those scenarios is good practice. You can’t force logical flow in a switch statement to move from one case to the next by omitting the break keyword; in other words, only one conditional block is processed within a switch statement. This means that logically, you can’t use the AND logical operator. However, you can leverage the OR logical operator. The following code demonstrates a case in which you want the same code to run for both the green and yellow background conditions: switch (canvas.style.backgroundColor) { case 'yellow': case 'green': alert('proceed'); break; case 'red': alert('stop'); break; default: alert('unknown condition'); break; }

In this code, multiple case statements are stacked onto each other. If any of the stacked case statements evaluates to true, the code block following that case statement is processed, thus implying a logical OR. You don’t need to explicitly use the logical OR operator (||) to leverage logical OR semantics. IMPORTANT  A VALID SWITCH STATEMENT

The values used in the case statement for the purposes of the evaluation must be expressed as a constant. For example, switching on an integer value to determine whether it’s divisible by another number won’t work because the case would require an expression instead of a constant value. For example, case x / 10: would be an invalid case statement. However, the switch statement itself can accept an expression to evaluate against all cases inside the switch block.

Using ternary operators The ternary operator is essentially a shorthand mechanism for an if statement. The syntax of the ternary operation is ? :

When the expression evaluates to true, the true part runs; otherwise, the false part runs. This code demonstrates using the ternary operator to check the background color of the canvas: canvas.style.backgroundColor == 'green' ? document.write('proceed') : document.write('stop');

116

CHAPTER 2

Implement program flow

Working with arrays Arrays are JavaScript objects and are created just like any other JavaScript object, with the new keyword: var anArray = new Array(); var anArray = new Array(5); var anArray = new Array('soccer', 'basketball', …, 'badminton');

This code example shows an Array object being instantiated and demonstrating the three available constructors. The first line creates an empty Array object without a default size. The second line creates an Array object with a default size. Each value in the array is undefined because nothing is assigned to it yet. The last example creates an array initialized with data. In addition to the object constructors, you can create an array as follows: var anArray = ['soccer', 'basketball', …,'badminton'];

Under the hood, JavaScript converts the anArray variable to the Array object type. After creating an array, you can access its elements by using square brackets following the variable name, as shown in this example: var anArray = new Array(5); anArray[1] = 'soccer'; alert(anArray[1]);

You access elements within an array by their indexed position. This example accesses the element at index position 1 and assigns a value to it. Arrays in JavaScript are zero-based, which means that the first element in the array is at index zero, not at index one. The last element is at index Array.length –1—in the preceding example, 5–1=4. Hence, the array element indexes are 0, 1, 2, 3, and 4. EXAM TIP

Sizing arrays is very dynamic. In the preceding example, even though the array is initially declared to have a length of 5, if you try to access the 10th element, the array automatically resizes to accommodate the requested length. The following example demonstrates this concept: var anArray = new Array(5); alert(anArray.length); anArray[9] = ‘soccer’; alert(anArray.length);

A multi-dimensional array can contain other arrays. The following code demonstrates this: var multiArray = new Array(3); multiArray[0] = new Array(3); multiArray[1] = new Array(3); multiArray[2] = new Array(3);



Objective 2.1: Implement program flow

CHAPTER 2

117

This example creates a two dimensional 3 × 3 array. Each array isn’t required to be the same size; this example was just coded that way. Accessing the elements of a two-dimensional array is much the same as accessing a one-dimensional array, but you use two indexes: multiArray[1][2] = 'ball sport';

This example assigns a value to the first index of the first dimension and the second index of the second dimension, as illustrated in Figure 2-1.

FIGURE 2-1  Layout of a two-dimensional array

Because arrays are objects, they expose a number of powerful methods to make working with them easier. The following sections explain each available method and property.

Using the length property The length property provides information on how long the array is—that is, how many elements the array has allocated at the time the property is evaluated. This property is useful for situations in which you need to iterate over an array or to show users how many items are in the array at a specific point in time, such as in a queue. The following example shows how to access the length property: var anArray = new Array(5); alert(anArray.length);

Many functions enable you to manipulate array contents quickly and easily. EXAM TIP

Some array methods affect the Array object directly, whereas other methods return a new Array object. For the exam, you must understand when each case is applicable.

Using the concat method The concat method combines two or more arrays into one array: var sports = new Array( 'football', 'cricket', 'rugby', 'tennis', 'badminton'); var moreSports = new Array('soccer', 'basketball', 'hockey'); var combinedSports = sports.concat(moreSports);

118

CHAPTER 2

Implement program flow

The array returned by the concat method and stored in the combinedSports variable contains all the elements from both arrays in sequence. The contents of the moreSports array appear after the elements of the sports array in this example.

Using the indexOf and lastIndexOf methods The indexOf method provides a way to find the index of a known element. The following code sample demonstrates this: var sports = new Array('soccer', 'basketball', 'hockey', 'football', 'cricket', 'rugby', 'tennis', 'badminton'); var index = sports.indexOf('football', 0);

This example calls the indexOf method to determine the index of the element ‘football’. The indexOf method accepts two parameters: what to search for and the index at which to begin searching. This example searches the entire array, so the search starts at index 0. The result from this call to the indexOf method is 3, because the element is found in the fourth position. If the element being sought isn’t found, the method returns a value of –1. The indexOf method uses the identity operator to check for equality, which means that if an array contains strings such as ‘1’, ‘2’, and ‘3’ and you’re searching for the integer 3, the result is –1 because the equality operation returns false for all elements in the array. The indexOf method searches in ascending index order. To search in descending order—that is, to search from the end of the array to the beginning—use the lastIndexOf method, which accepts the same parameters.

Using the join method The join method joins all the elements in an array into a single string separated by a specified string separator. For example, to convert an array of strings into a comma-separated list, you could use the following code: var sports = new Array('soccer', 'basketball', 'hockey', 'football', 'cricket', 'rugby', 'tennis', 'badminton'); var joined = sports.join(',');

The join method accepts a string as a parameter, which is the string used as a delimiter to separate the values in the array. The result is a string of all the elements separated by the string passed into the join method.

Using the reverse method The reverse method reverses the sequence of all elements in the array. This example reverses the sports array: var sports = new Array('soccer', 'basketball', 'hockey', 'football', 'cricket', 'rugby', 'tennis', 'badminton'); sports.reverse();



Objective 2.1: Implement program flow

CHAPTER 2

119

The method reverses all the items so that ‘soccer’ becomes the last item in the array and ‘badminton’ becomes the first item.

Using the sort method The sort method sequences the items in the array in ascending order. In the sports array, the sort would be alphabetical, as shown in the following example: var sports = new Array('soccer', 'basketball', 'hockey', 'football', 'cricket', 'rugby', 'tennis', 'badminton'); alert(sports.indexOf('soccer')); sports.sort(); alert(sports.indexOf('soccer'));

The result is that the sports array is now sorted. The alert boxes show the index of the ‘soccer’ element before and after the sort, demonstrating that the element has moved from position 0 to position 6 in the array.

Using the slice method The slice method takes out one or more items in an array and moves them to a new array. Consider the following array with the list of sports: var sports = new Array('soccer', 'basketball', 'hockey', 'football', 'cricket', 'rugby', 'tennis', 'badminton'); var someSports = sports.slice(1, 2);

The slice method takes two parameters: the indexes where the slice operation should begin and end. The ending index isn’t included in the slice. All copied elements are returned as an array from the slice method. In this example, because ‘basketball’ is at index 1 and the ending index is specified at index 2, the resulting array someSports contains only one element: ‘basketball’.

Using the splice method The splice method provides a way to replace items in an array with new items. The following code demonstrates this: var sports = new Array('soccer', 'basketball', 'hockey', 'football', 'cricket', 'rugby', 'tennis', 'badminton'); var splicedItems = sports.splice(1, 3, 'golf', 'curling', 'darts');

The splice method returns an array containing the items that are spliced out of the source array. The first parameter is the index in the array where the splice operation should start. The second parameter is the number of items to splice, starting from the index specified in the first parameter. The optional last parameter lists items that are to replace the items being spliced out. The list doesn’t have to be the same length as the items being spliced out. In fact, if the last parameter is omitted, the spliced items are simply removed from the array and not replaced. In this example, three items are replaced, starting at index 1. So, ‘basketball’, ‘hockey’, and ‘football’ are replaced with ‘golf’, ‘curling’, and ‘darts’.

120

CHAPTER 2

Implement program flow

Implementing special types of arrays JavaScript doesn’t natively provide a custom object to represent specialized collections or arrays. Instead, methods are provided on the Array object that allows you to implement various types of specialized collections such as a queue or a stack. A queue is essentially a first-in-first-out type of collection. Whenever items are added to the list, they should go at the end of the line. In contrast, a is a last-in-first-out type of collection in which the last item put on the stack is the first item you can take out of the stack. The array methods that facilitate this type of behavior are pop, push, shift, and unshift.

Using the pop and push methods The pop and push methods provide stack functionality. The push method appends the specified items to the end of the array. The pop method removes the last item from the array. The following code demonstrates the push method: var sports = new Array(); sports.push('soccer', 'basketball', 'hockey'); sports.push('football');

This code creates an Array object, and then inserts (pushes) three items into the array. The items are added to the stack in the same order in which they appear in the parameter list. Next, the code pushes one additional item onto the stack. The pop method removes and returns the last item in the array: var nextSport = sports.pop();

When this code runs, the nextSport variable holds the value ‘football’ because that was the last value added to the array. NOTE USING PUSH AND POP ON ANY ARRAY

You can use the pop and push methods in any context to add and remove items from the end of an array. The stack concept is useful but isn’t a confining mechanism that limits use of these methods to just stack arrays.

Using the shift and unshift methods The shift and unshift methods work in the exact opposite way from the pop and push methods. The shift method removes and returns the first element of the array, whereas the unshift method inserts new elements at the beginning of the array. The following code uses the shift and unshift methods: var sports = new Array(); sports.unshift('soccer', 'basketball', 'hockey'); sports.unshift('football'); var nextSport = sports.shift();



Objective 2.1: Implement program flow

CHAPTER 2

121

The net result of this code is exactly the same as for the pop and push code, except the operations occur at the front of the array instead of the end. In other words, this example still illustrates the stack functionality of “last in, first out.” Taken together, the two concepts you’ve just seen (pop/push and shift/unshift) can be combined to create the concept of a first-in-first-out queue. The following code demonstrates using a queue in which the front of the line is the beginning of the array and the end of the line is the end of the array: var sports = new Array(); sports.push('soccer'); sports.push('basketball'); sports.push('hockey'); var get1 = sports.shift(); sports.push('golf'); var get2 = sports.shift();

This code first pushes some items into the array. This means that each item is added to the end of the array. When an item is needed from the array, the shift method gets the first item out of the beginning of the array—the item at index 0. You can easily implement the opposite mechanism by using the unshift and pop methods, which would achieve the same results but enter and retrieve items from the opposite ends of the array from this example.

Using advanced array methods This section examines some of the more advanced array methods. These methods all involve the use of a callback, which you’ll examine in more detail in Objective 2.4, “Implement a callback.” If callbacks are a new concept to you, you should study that objective before completing this section. The Array object exposes methods that enable you to process custom logic on every single element in the array. The following sections demonstrate each method.

Using the every method The every method lets you process specific logic for each array element to determine whether any of them meet some condition. Look at following code: var evenNumbers = new Array(0, 2, 4, 6, 8, 9, 10, 12); var allEven = evenNumbers.every(evenNumberCheck, this); if (allEven) { … } else { … } function evenNumberCheck(value, index, array) { return (value % 2) == 0; }

122

CHAPTER 2

Implement program flow

In this code, assume that the evenNumber array is created with a list of what you expected to be all even numbers. To validate this, you can use the every method. The every method takes two parameters: ■■

The name of the function that should be processed for each element

■■

An optional reference to the array object

The evenNumberCheck function called for each item in the array returns true or false for each item, depending on whether it meets the desired criteria. In this example, the value is tested to ensure that it’s an even number. If it is, the function returns true; otherwise, it returns false. As soon as the every method gets the first false result for any item in the array, it exits and returns false. Otherwise, if all elements in the array return true, the every method returns true. In the preceding code sample, an if statement was added to evaluate the return value of the every method and take an appropriate action. In this example, the evenNumberCheck function returns false on the sixth item in the array, because 9 is an odd number, so the test for even fails.

Using the some method The some method works very much like the every method. The difference is that some checks only whether any item in the array meets the criteria. In this case, the some method returns true if the called function returns true for any single element. If all elements in the array return false, the some method returns false. By this definition, you can use some to achieve the exact opposite of the every method when the some method returns false. The following code is updated from the previous example so that it uses the some method: var evenNumbers = new Array(0, 2, 4, 6, 8, 9, 10, 12); var allEven = evenNumbers.some(evenNumberCheck, evenNumbers); if (allEven) { … } else { … } function evenNumberCheck(value, index, array) { return (value % 2) == 0; }

With the code updated to use the some method, the return result isn’t true, because some of the values in the array are even numbers. Had this result returned false, you would know that all the elements in the array were odd numbers.



Objective 2.1: Implement program flow

CHAPTER 2

123

Using the forEach method The forEach method enables an application to process some logic against each item in the array. This method runs for every single item and doesn’t produce a return value. forEach has the same signature as the other methods you’ve seen so far in this section. The following code demonstrates the forEach method: var sportsArray = ['soccer', 'basketball', 'hockey', 'football', 'cricket', 'rugby']; sportsArray.forEach(offerSport); function offerSport(value, index, array) { var sportsList = document.getElementById("sportsList"); var bullet = document.createElement("li"); bullet.innerText = value; sportsList.appendChild(bullet); }

In this sample, the code assumes that a list element on the HTML page is ready to be filled with the list of sports, each formatted as a child node. Each element in the list is passed to the function and added as an element. The array elements aren’t sorted in this case. You can chain the methods together to ensure that the elements are, for example, alphabetized: sportsArray.sort().forEach(offerSport);

Like with all the advanced methods shown thus far, the elements are passed to the function in ascending index order. So you could call the sort method and chain it together with the forEach method to ensure that the elements are displayed to the user in order.

Using the filter method The filter method provides a way to remove items for an array based on some processing done in the callback function. The filter method returns a new array containing the elements that are included based on a return value of true or false from the callback function. In the even number example, you can use the filter method to scrub the array and ensure that the program continues to use only an array that contains even numbers, as demonstrated here: var evenNumbers = new Array(0, 2, 4, 6, 8, 9, 10, 12); var allEven = evenNumbers.filter(evenNumberCheck, evenNumbers); //work with the even numbers.... function evenNumberCheck(value, index, array) { return (value % 2) == 0; }

In this example, the evenNumberCheck method is the same as the one used previously. However, rather than use the every or any method to determine the quality of the data with respect to containing only even numbers, the filter method simplifies the removal of the odd numbers. You can use any logic in the callback function to process the element and determine whether it should be included in the returned array, such as pattern matching or a database lookup.

124

CHAPTER 2

Implement program flow

Using the map method The map method enables you to replace values in the array. Every element in the array is passed to a callback function. The callback function’s return value replaces the value for the position in the array that was passed in. The following example demonstrates having every number in an array rounded off appropriately: var money = [12.8, 15.9, 21.7, 35.2]; var roundedMoney = money.map(roundOff, money); … function roundOff(value, position, array) { return Math.round(value); }

This example provides the square of a series of numbers: var numbers = [1, 2, 3, 4, 5, 6, 7, 8]; var squares = numbers.map(squareNumber, numbers); … function squareNumber(value, position, array) { return value * value; }

Using the reduce and reduceRight methods The reduce and reduceRight methods are recursive. Each result of the callback function is passed back into the callback method as the previous return value along with the current element to be passed in. This provides some interesting scenarios. The reduce method processes the elements of the array in ascending order, whereas the reduceRight processes the elements of the array in descending order. The following example demonstrates using the reduce method to calculate a factorial: var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var factorials = numbers.reduce(factorial); function factorial(previous, current) { return previous * current; }

In this function, the factorial for 10 is calculated. In the math world, it’s denoted as 10! EXAM TIP

Some advanced functions enable you to change the source array, whereas others don’t. This is an important aspect to keep clear.

Implementing iterative control flow You’ve seen how to use if statements to control program flow. Another concept you can use to control the flow of JavaScript programs is iterative flow control, which enables you to loop over a block of code many times. You’ve already seen some iterative operations when you



Objective 2.1: Implement program flow

CHAPTER 2

125

reviewed the advanced methods on the array object that use callbacks. There, the iterative flow control was built into the various array methods. In this section, you’ll examine the native iterative control statements, including for and while loops.

Using the for loop The for loop is useful in cases in which a block of code should run based on a deterministic number of items. In some cases, you might want to iterate over a list of items in an array or list; in other cases, you might want to run a block of code a specific number of times to perform some type of animation or to create a specific number of objects. The syntax of the for loop is as follows: for(;;) { }

The for loop needs three elements: a counter, an expression, and an increment. ■■

■■

■■

The counter variable holds the current number of times the loop has run. You need to initialize the counter appropriately, such as to 1 or 0. The expression element evaluates the counter against some other value. Its purpose is to set a limit to the number of times the for loop runs. For example, if you wanted the loop to run 10 times, you could initialize the counter to 0, and the expression would be counter < 10. Doing so would ensure that the loop would run only while the expression returns true, that is, while the counter variable is less than 10. As soon as the counter equals 10, loop processing would stop, and code processing would continue after the for loop. With the counter increment, the for loop must be told how to adjust the counter variable after each loop iteration. The increment can be positive or negative depending on how the loop is set up. You can set the increment so that the loop counts sequentially, or use mathematical operators to increment by a different value.

The code or body of the loop is a block of code surrounded by curly braces. This code section runs for each loop iteration. The following code samples demonstrate various ways to use a for loop. First, here’s a simple for loop that runs a block of code 10 times: for (var i = 0; i < 10; i++) { document.write(i); }

Notice that because the counter is starting at 0, the expression is to be less than 10. If the counter is to start at 1, the expression would be 0; i--) { document.write(i); }

126

CHAPTER 2

Implement program flow

In addition to using the increment or decrement operators, you can multiply or divide the counter value. The following code prints out a set of numbers that increase by a factor of 2 up to 100: for(var i= 1; i header { grid-column: 1; grid-column-span: 3; grid-row: 1; border: 1px dotted black; } #blogPage > footer { grid-column: 1; grid-row: 5; grid-column-span: 3; border: 1px dotted black; } #blogPage > article { grid-column: 2; grid-row: 2; grid-row-span: 3; border: 1px dotted black; } #blogPage > #calendar { grid-column: 3; grid-row: 3; border: 1px dotted black; } #blogPage > #blogRoll { grid-column: 3; grid-row: 4; border: 1px dotted black; } #blogPage > #aboutMe { grid-column: 1; grid-row: 2; grid-row-span: 3; border: 1px dotted black; } #blogPage > #bloghistory { grid-column: 3; grid-row: 2; border: 1px dotted black; } }



Objective 4.4: Create an animated and adaptive UI

CHAPTER 4

293

This produces the output in Figure 4-48. As long as the screen is at least 1,200 px wide, this layout will be applied. However, as the screen gets smaller, on a tablet for example, the user interface starts to get less user friendly. To accommodate a tablet screen, you can adjust the layout a bit by adding the following CSS code to the page: @media screen and (max-width: 1199px) and (min-width: 401px) { #blogPage { display: -ms-grid; grid-columns: 75% 1fr; grid-rows: (20%)[6]; width: 90%; height: 95%; border: 1px dotted black; margin: auto; } #blogPage > header { grid-column: 1; grid-column-span: 2; grid-row: 1; border: 1px dotted black; } #blogPage > footer { grid-column: 1; grid-row: 6; grid-column-span: 2; border: 1px dotted black; } #blogPage > article { grid-column: 1; grid-row: 3; grid-row-span: 3; border: 1px dotted black; } #blogPage > #calendar { grid-column: 2; grid-row: 4; border: 1px dotted black; } #blogPage > #blogRoll { grid-column: 2; grid-row: 5; border: 1px dotted black; } #blogPage > #aboutMe { grid-column: 1; grid-row: 2; grid-column-span: 2; border: 1px dotted black; } #blogPage > #bloghistory { grid-column: 2; grid-row: 3; border: 1px dotted black; } }

294

CHAPTER 4

Use CSS3 in applications

With this code, you can restructure the layout of the grid based on the different screen size. The output of this code produces a user interface shown in Figure 4-50.

FIGURE 4-50  The layout of the blog is adjusted for the tablet screen size

Now you have the desktop-size screen and tablet-size screen looking good. The next one to account for is the smaller smart phone screen. Add the following code to your CSS to place a media query for the smaller screen size:



@media screen and (max-width: 400px) { #blogPage { display: grid; grid-columns: 50% 50%; grid-rows: 15% 15% 1fr 15% 15%; width: 90%; height: 95%; border: 1px dotted black; margin: auto; } #blogPage > header { grid-column: 1; grid-column-span: 2; grid-row: 1; border: 1px dotted black; } #blogPage > footer { grid-column: 1; grid-row: 5; grid-column-span: 2; border: 1px dotted black; } #blogPage > article { grid-column: 1; grid-row: 3; grid-column-span: 2; border: 1px dotted black; } #blogPage > #calendar {

Objective 4.4: Create an animated and adaptive UI

CHAPTER 4

295

grid-column: 2; grid-row: 2; border: 1px dotted black; } #blogPage > #blogRoll { grid-column: 1; grid-row: 4; border: 1px dotted black; } #blogPage > #aboutMe { grid-column: 1; grid-row: 2; border: 1px dotted black; } #blogPage > #bloghistory { grid-column: 2; grid-row: 4; border: 1px dotted black; } }

As the screen size gets to be as small as it would be on a smart phone, the user interface will be rendered as shown in Figure 4-51.

FIGURE 4-51  The layout of the blog is adjusted for the screen size of a smart phone

296

CHAPTER 4

Use CSS3 in applications

NOTE  DOES IT WORK?

The media query does not require that a smart phone or tablet be in use, only that the view port of the page is within the query parameters. To test this code, simply resize your browser window beyond the defined sizes to see the page change its layout dynamically.

So far the CSS is working great. The only problem is you have a lot of CSS code in a single page. Typically, a website has multiple pages with styles shared across different pages. As a result, you will likely be linking an external CSS file to your HTML page. The link element supports media queries as well, which in turn lets you share a CSS file across multiple pages. For example, you might want to change the font-size of the text throughout your site based on the view port size. To achieve this, move the CSS from each media query into its own CSS file and link in your CSS files in the following way:

With the CSS linked in this fashion, you can add and modify the styles for the different view ports centrally for your entire website.

Hiding or disabling controls The ability to modify the user interface positioning using media queries as shown in the last section is very useful. In addition, some layouts might just not work in some view ports. In this case, you might want to complete hide controls or disable controls. HTML elements are visible by default. However, they can be made invisible by setting the visibility CSS property as shown in the following code: .myhiddenelements { visibility:hidden; }

By setting the visibility to hidden, the control is not visible to the end user of the webpage. When hiding an element using the visibility property, the overall layout still behaves as though the element is there. If you prefer to have the element hidden and the layout behave as though it is not there, the display property should be used as shown in the following code: .myhiddenelements { display: none; }

With the display property, the element is not visible and the layout is not affected by it.



Objective 4.4: Create an animated and adaptive UI

CHAPTER 4

297

If you do not want to hide the element but only make it disabled such that the user can see it but cannot perform any action on it, you need to add the attribute directly to the HTML element. As such, you can define a CSS class that you can apply to any elements that you want to have disabled: .disableIt

Now that you have a CSS class called .disableIt, you can apply this class to any elements that you want to disable. In this case, you want to disable a button element, so apply the class to the button element as shown here: My Button

The last step is to create some JavaScript that finds all the controls with this class assigned to it and adds the disabled attribute to them. The following code demonstrates this: $("document").ready(function (e) { $(".disableIt").attr("disabled", "disabled"); });

This script has the same net effect as putting the attribute directly on the button element as shown here: My Button

When you have many elements that you would like to disable, it is much easier to create a CSS class, apply it to the elements, then by using jQuery, apply the disabled attribute to them all.

Thought experiment Combining effects In this thought experiment, apply what you’ve learned about this objective. You can find the answer to this question in the “Answers” section at the end of this chapter. Consider combining transitions with transformations. Individually, transitions and transformations provide interesting effects to the HTML page. Consider what kind of effects can be achieved by using them together.

298

CHAPTER 4

Use CSS3 in applications

Objective summary ■■

■■

■■

■■

HTML elements can be manipulated with transitions using the transition-property, transition-duration, and transition-delay CSS properties. Elements can be manipulated in 2-D and 3-D space with effects such as translate, scale, rotate, and matrix. Media queries allow you to have a dynamic, responsive user interface based on the view port size and type. The visibility property hides a control but keeps its position in the overall layout. Using the display property to hide a control removes it from the layout as well.

Objective review Answer the following questions to test your knowledge of the information in this objective. You can find the answers to these questions and explanations of why each answer choice is correct or incorrect in the “Answers” section at the end of this chapter. 1. Which of the following statements will hide an element but keep its space in the over-

all flow? A. display: none; B. visibility: hidden; C. display: inline; D. visibility: visible; 2. Media queries are best suited for what purpose? A. Setting the priority of style sheet references in a webpage B. Creating a responsive user interface based on the screen size of the view port C. Modifying the view port to properly fit the content of the page D. Connecting to third-party style sheets to alter the layout 3. Which of the following transition-timing-function properties makes the transition start

slow, speed up, then end slow? A. ease B. ease-in C. ease-out D. ease-in-out



Objective 4.4: Create an animated and adaptive UI

CHAPTER 4

299

Objective 4.5: Find elements using CSS selectors and jQuery Objective 1.2, “Write code that interacts with UI controls,” covered the use of document selectors to find HTML elements by their unique name. In this section, you explore more advanced techniques to find elements through the use of CSS selectors and jQuery.

This objective covers how to: ■■

Define element, style, and attribute selectors

■■

Choose the correct selector to reference an element

■■

Find elements by using pseudo-elements and pseudo-classes

Defining element, style, and attribute selectors CSS uses selectors that you define in a CSS file or style block to identify which elements in a webpage the defined styles should be applied to. This can be done by specifying the element itself as the selector. For example, the following CSS is an element selector for the div element: div{ … }

Any div within scope of the declaration will have the styles applied to it. Another possible selector is a class selector. To use a class selector, you define a custom class name in the CSS file. This can be any name prefixed with a period. Then, any element that has that class assigned to it via the class attribute will have the defined styles applied. For example: .mycustomclass{ …. }

Another way to use CSS to select specific elements on the page is to use attribute selection. This is achieved by specifying an element type followed by a specific attribute. For example if you have a web form that needs to be filled in, you may attribute required fields with a red border around the textboxes. The following code achieves this for any elements that have the required attribute specified: input[required] { border: 1px red solid; }

There are other possibilities for the use of attribute selectors. These are outlined in Table 4-12.

300

CHAPTER 4

Use CSS3 in applications

TABLE 4-12 Attribute selector capabilities

Attribute selector

Description

=

Specifies that an attribute equals a specific value. For example, the URL of an anchor is a specify URL.

~=

Specifies a space-separated list of words as the values for an attribute.

^=

Specifies that the attribute has a value starting with the text specified.

$=

Specifies that the attribute has a value ending with the specified text.

*=

Specifies that the attribute has a value containing the specified text.

Choosing the correct selector to reference an element Choosing the correct selector to reference an element is an important consideration. You need to ensure that you organize your selectors and your elements such that only the desired elements are impacted by the defined styles. For example, the following style affects all article elements: article{ border-color: 1px solid red; }

If you do not want to affect all articles but only the newest article, you must distinguish them, perhaps by adding a custom CSS class to the definition and assigning this to only the newest article: article.newest{ border-color: 1px solid red; }

By specifying it this way, you are assured that not every article on the page is impacted by the style. This topic is covered in more detail in Objective 4.6 in the section “Referencing elements correctly.”

Finding elements by using pseudo-elements and pseudo-classes Pseudo-classes and pseudo-elements provide some very powerful ways to add styles to elements. Pseudo-classes allow you to apply styles to an element based on its state, its interaction with the user, or its position in the document. Pseudo-elements allow you to insert content into the page in locations relative to the elements that the CSS is being applied to. You will examine each of the common pseudo-classes and pseudo-elements in this section.



Objective 4.5: Find elements using CSS selectors and jQuery

CHAPTER 4

301

:link, :visited, and :hover These are the most commonly used pseudo-classes, used most frequently with the anchor element, providing a clickable link for the user of the webpage. With these pseudo-classes, you can control what styles are applied to a hyperlink in the different states. For example, the following CSS changes the color of the link based on its state: a:link { color: green; } a:hover { color: red; } a:visited { color: black; }

In this example, the link by default will be green. When a user moves the mouse over the link, the color of the link will change to red. If the user does not click the link and then moves off of it, the link will go back to green. However, if the user clicks the link, it becomes a visited link and will change to black.

:checked The :checked pseudo-class lets you apply styles to elements that are in a checked state. Elements that support this pseudo-class are check boxes and radio buttons. The amount of styling you can apply to the default elements is minimal. However, there are ample resources to customize these elements using CSS. The following example shows how to hide a check box when a user clicks it. input[type="checkbox"]:checked { display: none; }

:required The :required pseudo-class lets you apply styles to any elements on the page that have the required attribute. This is a convenient way to highlight required fields on a form. The following CSS demonstrates applying styles to all required input controls: input:required { border: 2px solid red; }

All required input controls will now have a red border to highlight this to the user.

302

CHAPTER 4

Use CSS3 in applications

:enabled and :disabled The :enabled and :disabled pseudo-classes allow you to style controls based on their enabled or disabled state. By default, disabled controls typically are light gray. With these pseudo classes, you can control how the element displays in either state. The following code demonstrates this: input:disabled { background-color: blue; } input:enabled { background-color: white; }

If a control is enabled, the background will be white; otherwise disabled controls will be blue.

:first-child The :first-child pseudo-element applies the specified styles to the first instance of the element that occurs in a list, for example, the first paragraph element in this HTML: Lorem Lorem Lorem Lorem

Ipsum Ipsum Ipsum Ipsum

... ... ... ...

The following CSS will change the text color to green in the first paragraph element: p:first-child { color:green; }

:first-letter The :first-letter pseudo-element will alter the style of the first letter in the specified element. Continuing with the example HTML above, the following CSS will increase the size of the first letter in each paragraph element: p::first-letter { font-size: xx-large; }



Objective 4.5: Find elements using CSS selectors and jQuery

CHAPTER 4

303

:before and :after The :before and :after pseudo-elements will add the specified content in front of or after the indicated element selector. So, the following code would add ** to the front and end of the paragraph element: p::before { content: '**'; } p::after { content: '**'; }

:first-line The :first-line pseudo-element alters the styles of the first line of a text element. The following CSS will make the first line of the text inside the paragraph element green and larger: p::first-line { color:green; font-size: x-large; }

Thought experiment Using jQuery with pseudo-classes In this thought experiment, apply what you’ve learned about this objective. You can find an answer to this question in the “Answers” section at the end of this chapter. Consider how you can use jQuery to select elements using pseudo-classes and pseudo-elements. Using only jQuery, apply CSS styles to the first paragraph of any group of paragraph elements.

Objective summary ■■

■■

■■

304

Pseudo-elements and pseudo-classes provide an advanced mechanism for searching HTML elements in a page and applying styles. Using pseudo-elements and pseudo-classes you can change the style of an element based on user actions. Using pseudo-elements and pseudo-classes you can gain granular control over parts of the text in a text block.

CHAPTER 4

Use CSS3 in applications

Objective review Answer the following questions to test your knowledge of the information in this objective. You can find the answers to these questions and explanations of why each answer choice is correct or incorrect in the “Answers” section at the end of this chapter. 1. Which one of the following is a CSS class selector? A. .code B. #code C. div[code] D. :code 2. Which one of the following is an attribute selector? A. .required B. #required C. input[required] D. :required 3. Which of the following statements would alter the style of an anchor element when

the mouse is moved over it? A. a:link B. a:mouseover C. a:beforeclick D. a:hover

Objective 4.6: Structure a CSS file by using CSS selectors CSS files can become large and complex. Structuring them in an organized fashion will make them easier to maintain and also to know what selectors are best suited to be used to reference the HTML on your page.

This objective covers how to:



■■

Reference elements correctly

■■

Implement inheritance

■■

Override inheritance using !important

■■

Style an element based on pseudo-elements and pseudo-classes

Objective 4.6: Structure a CSS file by using CSS selectors

CHAPTER 4

305

Referencing elements correctly CSS is used to apply styles to elements in an HTML page. To do so, the CSS has to know which elements to apply the styles to. There are a few ways to reference elements from CSS. This is known as the selector syntax. This has been demonstrated throughout the chapter. This section will explain specifically how to reference elements from CSS. The key consideration is to ensure that you reference elements such that the styles affect only the elements you want affected. In large complex CSS, it can get complicated. Elements can be referenced from CSS by their element name. For example: p{…} article{…} div{…}

In this code, styles are applied to all paragraph elements, article elements, and div elements. The next method to select elements is through the use of classes as shown here: .bold{…} .largeTitle{…}

In this code, the styles are applied only to HTML elements that have their class attribute assigned these class names. Element names and classes can be combined to narrow the selector even further: p.largeTitle{…}

This code applies the styles only to paragraph elements that have the class largeTitle assigned to the class attribute. The most granular method to reference HTML elements from CSS is by using the id or name of the element: #nameBox{…}

This code applies the specified styles only to the single element on the page with the specified name. In some cases, you might want to apply the same style to many elements of different types. In this case, you can group them and define the styles only once: p, H1, H2 {…}

In this sample, all three of the HTML elements noted will have the defined styles applied to them.

306

CHAPTER 4

Use CSS3 in applications

Implementing inheritance Some styles applied to a parent element are automatically inherited by children elements. For example, if a series of paragraph elements are inside an article element, and font and text styles are applied to the article, all the paragraph elements will automatically inherit the font and text styles as well. The following code demonstrates this concept: div { font-family: sans-serif; color: green; }

And with the following HTML: hello div world. Hello paragraph world.

Both the div and the paragraph will have the font and color styles applied to them because the paragraph element does not have any of its own styles defined. If you assign styles to the paragraph element to override the div styles you would be able to prevent the inheritance of the styles, as shown here: div { font-family: sans-serif; color: green; } p { font-family: serif; color: blue; }

Overriding inheritance using !important CSS for large websites can be complicated. Large websites may have CSS coming from different sources. It could be on each page and referenced externally. External libraries are more and more common as experts throughout the community have created themes that can be imported into your web applications. With all this styling coming from different sources, inheritance of styles can be tricky. In some cases you may just need to override all other com-



Objective 4.6: Structure a CSS file by using CSS selectors

CHAPTER 4

307

peting styles completely with your own desired style. This is where the !important keyword comes in. Consider the following simple CSS example: p { font-family: serif; color: blue; } p { color: purple; } p{ color: yellow; }

In this CSS code, you have three competing styles for the paragraph element. How the browser renders this is based on the last style it reads for an element it applies to. So in this case, the text in all the paragraphs will be yellow. However, if you want to override this behavior and force the paragraph elements on the page to be purple, you simply add the !important keyword to the style you want to have applied: p{ font-family: serif; color: blue; } p{ color: purple !important; } p{ color: yellow; }

The paragraph elements will render purple and not yellow. The !important notation tells the parser to give that style priority. This is a simplistic example, but the concept is the same whether the styles are in a single page such as this or if they come from a variety of external sources with conflicting styles.

Styling an element based on pseudo-elements and pseudo-classes The Objective 4.5 section, “Finding elements by using pseudo-elements and pseudo-classes” demonstrated the use of pseudo-elements and pseudo-classes as selectors. In addition, that section can be referenced for how to apply styles to elements based on the use of the pseudo-class and pseudo-element selectors.

308

CHAPTER 4

Use CSS3 in applications

Thought experiment Combining pseudo-element selectors In this thought experiment, apply what you’ve learned about this objective. You can find an answer to this question in the “Answers” section at the end of this chapter. Consider how you can get very specific styles applied to your HTML elements through the combination of pseudo-classes and pseudo-elements. For example, how would you change the appearance of the first letter of the first paragraph of each group of paragraphs on the page but only when the user hovers over it? Experiment with other combinations of selectors to achieve specific effects.

Objective summary ■■

Referencing elements correctly takes careful consideration of how you will structure your CSS and your HTML elements.

■■

Selectors can be nested and joined together to get more specific.

■■

HTML elements inherit styles automatically from their parent elements.

■■

■■

CSS is processed from the top down, so that last style processed wins if it conflicts with other style declarations. !important can be used to ensure that the desired style is rendered when there is a competing CSS declaration.

Objective review Answer the following questions to test your knowledge of the information in this objective. You can find the answers to these questions and explanations of why each answer choice is correct or incorrect in the “Answers” section at the end of this chapter. The review questions use the following HTML listing (line numbers are for reference only): 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15.



Objective 4.6: Structure a CSS file by using CSS selectors

CHAPTER 4

309

16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31.

1. Referencing the HTML listing, how would you style only the first paragraph inside the

footer element to have a smaller font size? A. footer p:first-child { font-size: x-small; }

B. footer p.first-child { font-size: x-small; }

C. Footer:p:first-child { font-size: x-small; }

D. Footer=>p,first-child { font-size: x-small; }

310

CHAPTER 4

Use CSS3 in applications

2. Referencing the HTML listing, how would you apply a new font to all the H1 elements?

In addition, the elements in an article should be italic. A. h1 { font-family: ‘Courier New’; article h1 { font-style:italic; } }

B. h1 { font-family: ‘Courier New’; } article h1 { font-style:italic; }

C. h1 { font-family: ‘Courier New’; font-style:italic; } article h1 { font-style:italic; }

D. h1 { font-family: ‘Courier New’; } article, h1 { font-style:italic; }

3. Referencing the preceding HTML listing, write the CSS code to apply a border to the

aside element that is 100 pixels high and 50 pixels wide. In addition, provide a shadow effect and slightly skew the element to the right 5 degrees.



Objective 4.6: Structure a CSS file by using CSS selectors

CHAPTER 4

311

Answers This section contains the solutions to the thought experiments and answers to the objective review questions in this chapter.

Objective 4.1: Thought experiment jQuery is the easiest way to achieve changing CSS styles dynamically. JavaScript will do the trick as well. All the CSS properties that have been looked at are available to be changed dynamically. So for example, in the following code, the color of the button is changed to green when it is clicked: $("document").ready(function () { $("#changeStyle").click(function () { $(this).css("color", "green"); }); }); Change Style

Objective 4.1: Review 1.

Correct answer: B A. Incorrect: font-style: italic will display the text with italics. B. Correct: font-weight: heavy; heavy is not a valid option for font-weight. C. Incorrect: font: bolder 12px arial; is a valid shorthand for setting font attributes. D. Incorrect: color: green; is a valid way to change the color of the text to green.

2. Correct answer: D A. Incorrect: Right will align all the text to the right side of the box. B. Incorrect: Full is not a valid option. C. Incorrect: Center will align the text along the center of the box. D. Correct: Justify will align the text such that each line takes up the width of the box.

312

CHAPTER 4

Use CSS3 in applications

3. Correct answer: C A. Incorrect: word-margin is not a valid option. B. Incorrect: letter-margin is not a valid option. C. Correct: word-spacing will set the amount of space between words. D. Incorrect: word-padding is not a valid option.

Objective 4.2: Thought experiment The following code demonstrates how to alter an element’s position using jQuery. p { position:fixed; left: 1px; } var pos = 1; $("document").ready(function () { setInterval(function () { var newPos = (pos + 1) + "px"; $("#scrollMe").css("left", newPos); pos += 1; }, 20); }); This text moves accross the screen.

Objective 4.2: Review 1. Correct answer: D A. Incorrect: div{height: 50px; width: 50%;} is valid. B. Incorrect: div{height: 50px; width: 50px;} is valid. C. Incorrect: div{height: 50cm; width: 50px; is valid. D. Correct: div{height: 50ft; width: 50ft;} ft is not a valid unit of measurement.

Answers

CHAPTER 4

313

2. Correct answer: A A. Correct: Each side will be styled differently and the syntax is correct. B. Incorrect: border-sides is not a valid property. C. Incorrect: border-all is not a valid property. D. Incorrect: The syntax is not correct to set the border properties with the short-

hand. Full is not a valid value. 3. Correct answer: B A. Incorrect: This is not the correct sequence. B. Correct: Margin, border, padding is the correct sequence. C. Incorrect: This is not the correct sequence. D. Incorrect: This is not the correct sequence. 4. Correct answer: A A. Correct: box-shadow: gray 5px 5px; will apply a box shadow to the right and bot-

tom edge of a div element. B. Incorrect: The shadow will be on the left and bottom. C. Incorrect: The shadow will be on the top and right. D. Incorrect: The shadow will be the top and left. 5. Correct Answer: B A. Incorrect: Absolute positioning is relative to the parent. B. Correct: Fixed positioning is the correct answer. C. Incorrect: Relative positioning is relative to the elements in normal flow.

Objective 4.3: Thought experiment The following listing shows the code for the blog page with the addition of a column layout for the main content section. The additional CSS is highlighted in bold. html, body { height: 100%; width: 100%; } #blogPage { display: grid; grid-columns: 15% 1fr 25%; grid-rows: (20%)[5]; width: 90%; height: 95%;

314

CHAPTER 4

Use CSS3 in applications

border: 1px dotted black; margin: auto; } #blogPage > header { grid-column: 1; grid-column-span: 3; grid-row: 1; border: 1px dotted black; } #blogPage > footer { grid-column: 1; grid-row: 5; grid-column-span: 3; border: 1px dotted black; } #blogPage > article { grid-column: 2; grid-row: 2; grid-row-span: 3; border: 1px dotted black; } #blogPage > #calendar { grid-column: 3; grid-row: 3; border: 1px dotted black; } #blogPage > #blogRoll { grid-column: 3; grid-row: 4; border: 1px dotted black; } #blogPage > #aboutMe { grid-column: 1; grid-row: 2; grid-row-span: 3; border: 1px dotted black; } #blogPage > #bloghistory { grid-column: 3; grid-row: 2; border: 1px dotted black; } #cols { width: 80%; height: 100%; border: 1px solid black; column-count: 3; column-rule-color: black; column-rule-style: dashed; column-rule-width: 2px; column-gap: 5px; } hgroup { column-span: all; text-align: center; }

Answers

CHAPTER 4

315

My Blog Header My Blog's Main Body Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna liquam … My Blog Footer A calendar My favorite blogs Who am I? My blog history

Objective 4.3: Review 1. Correct answer: C A. Incorrect: Flexbox lays elements out in a flow direction. B. Incorrect: Multi-column lays elements out only in columns. C. Correct: Grid layout allows layout in rows and columns. D. Incorrect: Exclusions do not deal with the layout in rows and columns. 2. Correct answer: D A. Incorrect: This is a true statement. B. Incorrect: This is a true statement. C. Incorrect: This is a true statement. D. Correct: Grid layouts can be set into rows and columns. 3. Correct answer: A A. Correct: both will allow the text to wrap along both sides of an element. B. Incorrect: all is not a valid value. C. Incorrect: left and right is not a valid value. D. Incorrect: cross is not a valid value.

316

CHAPTER 4

Use CSS3 in applications

Objective 4.4: Thought experiment By combining the transition with the transform, you are able to create effects that you otherwise would not be able to create. For example, if you want to create the effect of a box spinning or rotating, you can apply a transform: rotate(360deg); to the box. However, this happens so fast that you cannot see that the box rotated. Alternatively, by applying a transform effect with a longer duration, you actually can see the box rotate. Look at the following code, which demonstrates this effect on a series of div boxes: div { margin: 10px 10px 10px 10px; height: 50px; width: 50px; background-color: red; transition: transform 3s; } div:hover { transform: rotate(360deg); }

Objective 4.4: Review 1. Correct answer: A A. Correct: display:none; will hide an element but keep its space in the overall flow. B. Incorrect: visibility: hidden; will maintain the element in the normal flow. C. Incorrect: display: inline; will show the element in the normal flow. D. Incorrect: visibility: visible; will show the element in the normal flow.

Answers

CHAPTER 4

317

2. Correct answer: B A. Incorrect: Media queries are not best suited for setting the priority of style sheet

references in a webpage. B. Correct: Media queries are best suited for creating a responsive user interface

based on the screen size of the view port. C. Incorrect: Media queries are not best suited for modifying the view port to prop-

erly fit the content of the page. D. Incorrect: Media queries are not best suited for connecting to third-party style

sheets to alter the layout. 3. Correct answer: A A. Correct: ease will start slow, speed up and end slow. This is also the default value. B. Incorrect: ease-in will start slow and speed up. C. Incorrect: ease-out will slow near the end. D. Incorrect: ease-in-out will slow at the beginning and at the end.

Objective 4.5: Thought experiment What is interesting about this thought experiment is recognizing that not all pseudo-class or pseudo-element selectors are supported by jQuery. For example, inserting content via pseudo selectors does not work in this way. However, some things can be done via jQuery. In this example, the following code shows how the pseudo-element selector works in a jQuery selector: $("document").ready(function () { $("p:first-child").css("color", "green"); });

Objective 4.5: Review 1. Correct answer: A A. Correct: .code is a CSS class selector. B. Incorrect: #code is an ID selector. C. Incorrect: div[code] is an attribute selector. D. Incorrect: :code is not a valid statement.

318

CHAPTER 4

Use CSS3 in applications

2. Correct answer: C A. Incorrect: .required is a class selector. B. Incorrect: #required is an ID selector. C. Correct: input[required] is an attribute selector. D. Incorrect: :required is a pseudo-class that would be combined with an element

selector. 3. Correct answer: D A. Incorrect: a:link would specify the styles for an unvisited hyperlink. B. Incorrect: a:mouseover is not a valid pseudo-class. C. Incorrect: a:beforeclick is not a valid pseudo-class. D. Correct: a:hover will change the style when the user moves the mouse over the

link.

Objective 4.6: Thought experiment When you want to achieve specific formatting on complex webpages, you need to account for the hierarchy of your page and ensure that you understand how the inheritance is going to impact the nested HTML elements’ styles. The more specific your selectors, the more CSS you need to write but have more control. The following code will demonstrate a specific selector that will alter only the first character of the first paragraph when a user hovers over it: p:first-child:hover:first-letter { font-size: xx-large; }

This code does not seem like much, but it is very specific. It will override default inheritance and any other styles defined for that same element. Consider the following CSS: p:first-child:first-letter { font-size: xx-small; } p:first-child:hover:first-letter { font-size: xx-large; }

The first selector will default the size of the first letter to xx-small but the hover class will override this. This is where it may be desirable to use the !important keyword to force the xxsmall font. In this example, the two styles purely conflict. You would need to choose one over the other.

Answers

CHAPTER 4

319

Objective 4.6: Review 1. Correct answer: A A. Correct: This is the correct syntax specifying the first paragraph child of the footer

element to have a smaller font. B. Incorrect: The p.firstchild is not the correct syntax. It would need to be

p:first-child. C. Incorrect: The colon after the footer is not correct syntax. D. Incorrect: The => notation is not the correct syntax. 2. Correct answer: B A. Incorrect: Styles cannot be nested inside each other in this way. B. Correct: It is correct to first specify the H1 style, then specify the H1 styles for

H1 elements that are beneath an article element. C. Incorrect: This will make all H1 elements italic. D. Incorrect: This will make all H1 elements italic as the comma after article creates a

list of elements to apply the styles to. 3. Correct Answer: aside { height:100px; width:50px; border: 2px solid black; transform: skew(-5deg); box-shadow: 5px 5px; }

320

CHAPTER 4

Use CSS3 in applications

Index

Symbols 2-D transformations, creating an animated UI,  291–292 3-D transformations, creating an animated UI,  291–292 @font-face keyword,  238

A Abort method,  220 absolute positioning,  61, 259–262 accessing data consuming data,  218–223 serializing, deserializing, and transmitting data,  224–228 binary data,  225–228 JSON data,  225 XMLHttpRequest object,  224–225 validating user input, HTML5 elements,  190–209 content attributes,  206–209 input controls,  190–206 validating user input, JavaScript,  211–217 built-in functions,  216 preventing code injection,  216–217 regular expressions,  211–215 addColorStop method,  51 addEventListener method,  133–134, 146 adding HTML5 elements,  22–34 altering the DOM,  28–34 DOM (Document Object Model),  22–23 selecting items in the DOM,  23–28 advanced arrays,  122–125 :after pseudo-element,  304 AFunction method,  89 AJAX, creating dynamic webpages,  161–165 AJAX call, parameters,  164–165 alignment, applying to text,  240 allow-forms value (sandbox attribute),  217

allow-same-origin value (sandbox attribute),  217 allow-top-navigation value (sandbox attribute),  217 altering the DOM,  28–34 altKey property, keyboard events,  141 animated UI (CSS3), creating,  288–298 2-D and 3-D transformations,  291–292 adjusting UI based on media queries,  292–297 CSS transitions,  289–290 hiding/disabling controls,  297–298 anonymous functions callbacks,  167–169 event handling,  134–135 APIs (Application Program Interfaces), 72-84 AppCache API,  77–80 Geolocation API,  81–84 WebSocket, bidirectional communication,  157–161 Web Storage,  72–77 Web Worker,  172–177 AppCache API,  77–80 appearance styling HTML box properties,  244–249 altering size,  244–245 bordering,  245–247 padding and margin,  247–249 styling HTML text properties bold, 237 color,  236–237 italic, 238 appendChild method,  29 Application Programming Interfaces.  See APIs applying styles elements,  60–68 changing location of element,  61–63 showing/hiding elements,  67–68 transforms,  63–67 HTML box properties,  244–263 appearance attributes,  244–249 element position,  258–263 graphic effects,  249–258 321

arc method

HTML text properties,  235–242 alignment, 240 bold, 237 color,  236–237 fonts,  238–239 hyphenation,  241–242 italic, 238 spacing, 241 arc method,  43 arrays,  117–120 advanced,  122–125 concat method,  118–119 indexOf and lastIndexOf methods,  119 join method,  119 length property,  118–119 reverse method,  119–120 slice method,  120 sort method,  120 special types,  121–122 splice method,  120 element,  8–9 element,  11–12 assignment event handling,  133–134 attribute selectors (CSS3),  301 element,  38–39 autoplay attribute, element,  35 auto value hyphen property,  241 wrap-flow property,  277

B background-image property,  250–251 background images, styling HTML apps,  250–251 :before pseudo-element,  304 beginPath method,  41, 47 behavioral program flow,  111 Bezier curves,  46–47 bezierCurveTo method,  43 BFunctionWithParam method,  89 bidirectional communication, WebSocket API,  157–161 binary data, serializing and deserializing,  225–228 blank element,  40 blur events,  140 blur parameter box-shadow property,  252–253 text-shadow property,  256 bold, applying to text,  237 border-color property,  245 322

bordering, styling HTML apps,  245–247 border-spacing property,  246 border-style property,  245 border-width property,  246 both value, wrap-flow property,  277 Bottom property, positioning elements,  61 box properties, styling HTML apps,  244–263 appearance attributes,  244–249 altering size,  244–245 bordering,  245–247 padding and margin,  247–249 element position,  258–263 graphic effects,  249–258 background images,  250–251 clipping,  257–258 gradients,  251–252 shadow effects,  252–257 transparency/opacity,  249–250 box-shadow property,  252 break keyword,  115, 129 bubbled events,  136–138 built-in functions, validating user input,  216 button input element, validating user input,  203–206

C cache parameter, AJAX calls,  164 CACHE section, AppCache API manifest file,  78 callbacks,  156–170 anonymous functions,  167–169 bidirectional communication with WebSocket API,  157–161 dynamic webpages, jQuery and AJAX,  161–165 this pointer,  169–170 wiring events with jQuery,  165–167 canceling events,  135–136 element,  39–58 drawing curves,  43–47 drawing images,  52–53 drawing lines,  41–43 drawing text,  53–55 fill method,  49–52 path methods,  47–48 rect method,  48–49 CanvasGradient objects, filling,  50–52 caret, regular expressions,  212 cascading style sheets.  See CSS3 (cascading style sheets) case statements,  115

creating

catch block (try...catch...finally constructs),  150 center value, text-align property,  240 change events,  139–140 changing location of elements,  61–63 checkbox input type, validating user input,  200–201 :checked pseudo-class,  302 checking for null values, exception handling,  154 checking value, app cache status property),  79 childNodes property,  31 clear method,  73 clear value, wrap-flow property,  277 click events,  141 clientX property, mouse events,  142 clientY property, mouse events,  142 clipping, styling HTML apps,  257–258 clip property,  257–258 CLOSED value, readyState property),  161 close method, WebSocket objects,  161 closePath method,  47 CLOSING value, readyState property,  161 code, writing code to interact with UI controls,  22–58 adding/modifying HTML elements,  22–34 implementing graphics with element,  39–58 implementing media controls,  34–39 SVG (Scalable Vector Graphics),  55–58 code injection, preventing,  216–217 collapse value, visibility property,  68 color, applying to text,  236–237 color input element,  193 color parameter box-shadow property,  252 text-shadow property,  256 color property,  236 color stop...n paramater, linear-gradient function,  252 column-count property, multi-columns,  274 column-gap property, multi-columns,  274 column-rule-color property, multi-columns,  274 column-rule property, multi-columns,  274 column-rule-style property, multi-columns,  274 column-rule-width property, multi-columns,  274 column-span property, multi-columns,  274 Columns property, multi-columns,  274 column-width property, multi-columns,  274 combining transformations,  67 concat method, arrays,  118–119 conditional operators,  112 conditional program flow,  111 configuration options, background images,  250

configuring timeouts and intervals, web worker process, 180 CONNECTING value, readyState property,  161 consuming data,  218–223 JSON and XML,  219 XMLHttpRequest object,  219–223 content attributes, validating user input,  206–209 pattern specification,  207–208 placeholder attribute,  208 read-only controls,  206–207 required controls,  208–209 spell checker,  207 content elements ,  8–9 ,  9–10 content layout,  266–287 flexible box model,  266–273 grid alignment,  280–285 multi-column layout,  273–276 position, floating, and exclusions,  276–279 regions, grouping, and nesting,  286–287 control2X parameter, bezierCurveTo method,  46 control2Y parameter, bezierCurveTo method,  46 controls, input.  See input controls controls attribute, element,  35 controlX parameter,  45, bezierCurveTo method,  46 controlY parameter,  45, bezierCurveTo method,  46 coordinate system, element,  40 coords property,  83 core structure, HTML5 page,  3–4 counterclockwise parameter, drawing arcs,  44 counter increment, for loops,  126 counter variable, for loops,  126 createPattern method,  52 createRadialGradient method,  51 creating animated UI (CSS3),  288–298 2-D and 3-D transformations,  291–292 adjusting UI based on media queries,  292–297 CSS transitions,  289–290 hiding/disabling controls,  297–298 custom events,  146 document structure,  2–19 HTML5 semantic markup,  2–15 layout containers,  15–16 optimizing for screen readers,  17–19 optimizing for search engines,  16–17 dynamic webpages, jQuery and AJAX,  161–165 objects and methods,  93–100 custom objects,  95–98 323

CSS3 (cascading style sheets)

inheritance,  99–100 native objects,  94 web worker process,  172–180 configuring timeouts and intervals,  180 using web workers,  178–179 Web Worker API,  172–177 web worker limitations,  179–180 CSS3 (cascading style sheets) creating an animated UI,  288–298 2-D and 3-D transformations,  291–292 adjusting UI based on media queries,  292–297 CSS transitions,  289–290 hiding/disabling controls,  297–298 finding elements using CSS selectors and jQuery,  300–304 flexible content layout,  266–287 flexible box model,  266–273 grid alignment,  280–285 multi-column layout,  273–276 position, floating, and exclusions,  276–279 regions, grouping, and nesting,  286–287 structuring CSS files using CSS selectors,  305–308 styling HTML box properties,  244–263 text properties,  235–242 CSS files, structuring with CSS selectors,  305–308 CSS selectors finding elements,  300–304 structuring CSS files,  305–308 CSS transitions, animating objects,  289–290 ctrlKey property, keyboard events,  141 cubic-bezier value, transition-timing property,  290 currentTime method, element,  38 curves, drawing with element,  43–47 customEventHandler function,  146 CustomEvent object constructor,  146 custom events,  146 custom image elements, element,  37 custom objects, creating and implementing,  95–98

D data access and security consuming data,  218–223 serializing, deserializing, and transmitting data,  224–228 validating user input, HTML5 elements,  190–209 validating user input, JavaScript,  211–217 324

dynamic, 189 static, 189 date input element,  193 datetime input element,  193 dblclick events,  142 declarative event handling,  132 declaring bubbled events,  136–138 variables, 87 default audio controls,  39 default media controls,  36 deserializing data,  224–228 binary data,  225–228 JSON data,  225 determinate progress tasks,  13 direction parameter, linear-gradient function,  252 :disabled pseudo-class,  303 disabling controls, CSS3,  297–298 dispatchEvent method,  146 display property,  67, 297 element,  15–16,  266–267 document.createElement method,  29 Document Object Model (DOM) adding/modifying HTML5 elements,  22–23 altering,  28–34 selecting items,  23–28 documents, creating structure,  2–19 HTML5 semantic markup,  2–15 layout containers,  15–16 optimizing for search engines,  16–17 screen readers,  17–19 DOM (Document Object Model) adding/modifying HTML5 elements,  22–23 altering,  28–34 events,  139–146 change events,  139–140 drag-and-drop functionality,  143–146 focus events,  140–141 keyboard events,  140–141 mouse events,  141–143 selecting items,  23–28 do...while loops,  128–129 downloading value (app cache status property),  79 drag-and-drop functionality, DOM events,  143–146 dragend events,  143 dragenter events,  143 drag events,  143 dragleave events,  143 dragover events,  143 dragstart events,  143

events

drawImage method,  52 drawing, element curves,  43–47 images,  52–53 lines,  41–43 text,  53–55 drop events,  143 dynamic data,  189 dynamic webpages, creating with jQuery and AJAX,  161–165

E ease-in-out value, transition-timing property,  290 ease-in value, transition-timing property,  290 ease-out value, transition-timing property,  290 ease value, transition-timing property,  290 elements adding/modifying,  22–34 altering the DOM,  28–34 DOM (Document Object Model),  22–23 selecting items in the DOM,  23–28 applying styles,  60–68 changing location of element,  61–63 showing/hiding elements,  67–68 transforms,  63–67 ,  8–9 ,  11–12 ,  15–16,  266–267 ,  12–13 ,  12–13 Finding, CSS selectors and jQuery,  300–304 and ,  5 , 7 , 14 , 6 position, styling HTML apps,  258–263 ,  13–14 ,  9–10 ,  15–16 validating user input,  190–209 button element,  205–206 button input type,  203–205 checkbox input type,  200–201 content attributes,  206–209 email input type,  198–199 password input control,  197–198 pattern specification,  207–208 placeholder attribute,  208

radio input type,  201–202 range input type,  202–203 read-only controls,  206–207 required controls,  208–209 spell checker,  207 text and textarea input types,  193–195 url input type,  195–197 writing code to interact with UI controls,  22–58 adding/modifying HTML elements,  22–34 implementing graphics with element,  39–58 implementing media controls,  34–39 SVG (Scalable Vector Graphics,  55–58 element selectors (CSS3),  300–301 else keyword,  113 email input type, validating user input,  198–199 :enabled pseudo-class,  303 enableHighAccuracy property (PositionOptions object),  81 endAngle parameter, drawing arcs,  44 end value, wrap-flow property,  277 endX parameter, bezierCurveTo method,  45-46 endY parameter,  bezierCurveTo method,  45-46 equality operators,  113 error handling, XMLHttpRequest object,  221 error method,  83 eval function, preventing code injection,  217 evaluating expressions,  112–116 if statements,  113–115 switch statements,  115–116 ternary operators,  116 evenNumberCheck method,  123, 124 event bubbling,  138 event listeners,  131 event objects,  131–132 events oncached, 80 onchecking, 80 ondownloading, 80 onerror, 80 onnoupdate, 80 onobsolete, 80 onprogress, 80 onreadystatechange, 219 ontimeout, 219 onupdateready, 80 raising and handling,  130–147 addEventListener and removeEventListener methods,  133–134 anonymous functions,  134–135 325

every method

assignment event handling,  133–134 bubbled events,  136–138 canceling events,  135–136 custom events,  146 declarative event handling,  132 DOM events,  139–146 event objects,  131–132 wiring with jQuery,  165–167 every method,  122–123 exception handling,  111, 149–154 checking for null values,  154 try...catch...finally constructs,  149–154 exclusions, flexible content layout,  276–279 expression element, for loops,  126 expressions evaluating,  112–116 if statements,  113–115 switch statements,  115–116 ternary operators,  116 regular, validating user input,  211–215

F FALLBACK section, AppCache API manifest file,  78 element,  12–13 element,  12–13 fill method, element,  49–52 fillRect method,  49 fillStyle property,  50 filter method,  124 finally block (try...catch...finally constructs),  151 :first-child pseudo-element,  303 firstChild property,  31 :first-letter pseudo-element,  303 :first-line pseudo-element,  304 fixed positioning,  258–259 flex-direction style, flexible box,  267 flexible box model, content layout,  266–273 flexible content layout,  266–287 flexible box model,  266–273 grid alignment,  280–285 multi-column layout,  273–276 position, floating, and exclusions,  276–279 regions, grouping, and nesting,  286–287 flex-pack style, flexible box,  267 flex-wrap property,  272–273 float flexible content layout,  276–279 property,  262–263 326

flow-from property,  286 flow-into property,  286 focus events,  140–141 focusin events,  140 focusout events,  140 font-family property,  238 font object applying bold to text,  237 applying italic to text,  238 fonts, applying styles to,  238–239 font-size property,  239 font typeface property,  238 font-weight CSS property,  237 element,  5 forEach method,  124 for...in loops,  127 for loops,  126–127 Form.Submit method, serializing and deserializing binary data,  226–227

G Geolocation API,  81–84 getAllResponseHeaders method,  220 getCurrentPosition method, Geolocation API,  81–83 getElementById method,  23, 25, 30, 60 getElementsByClassName method,  23, 26 getElementsByTagName method,  23, 26 getItem method,  73 getResponseHeader method,  220 global namespace, avoiding use when establishing object scope,  90–91 global scope variables,  87 gradients filling CanvasGradient objects,  50–52 styling HTML apps,  251–252 graphic effects, styling HTML apps,  249–258 background images,  250–251 clipping,  257–258 gradients,  251–252 implementing element,  39–58 drawing curves,  43–47 drawing images,  52–53 drawing lines,  41–43 drawing text,  53–55 fill method,  49–52 path methods,  47–48 rect method,  48–49 shadow effects,  252–257

HTML5 semantic markup

transparency/opacity,  249–250 grid alignment, content layout,  280–285 grid-column-span property,  285 grid-columns property,  282 grid-row-span property,  285 grid-rows property,  282 grouping content,  286–287

H handling errors, XMLHttpRequest object,  221 events,  130–147 addEventListener and removeEventListener methods,  133–134 anonymous functions,  134–135 assignment event handling,  133–134 bubbled events,  136–138 canceling events,  135–136 custom events,  146 declarative event handling,  132 DOM events,  139–146 event objects,  131–132 exceptions,  111, 149–154 checking for null values,  154 try...catch...finally constructs,  149–154 hasChildNodes property,  31 element,  5 height attribute, element,  35 height parameter, rect method,  48 hexadecimal value, color property,  236 element,  7 hidden value, visibility property,  68 hiding controls, CSS3,  297–298 elements,  67–68 hooking up events,  131 :hover pseudo-class,  302 h-shadow parameter box-shadow property,  252 text-shadow property,  256 HTML5 APIs,  72–84 AppCache API,  77–80 Geolocation API,  81–84 Web Storage,  72–77 HTML5 documents creating and implementing objects and methods,  93–100 custom objects,  95–98

inheritance,  99–100 native objects,  94 creating structure,  2–19 HTML5 semantic markup,  2–15 layout containers,  15–16 optimizing for search engines,  16–17 screen readers,  17–19 establishing scope of objects and variables,  86–91 avoiding use of global namespace,  90–91 leveraging the this keyword,  91 lifetime,  87–90 HTML5 APIs,  72–84 AppCache API,  77–80 Geolocation API,  81–84 Web Storage,  72–77 HTML5 elements.  See also HTML5 semantic markup adding/modifying,  22–34 altering the DOM,  28–34 DOM (Document Object Model),  22–23 selecting items in the DOM,  23–28 applying styles,  60–68 changing location of element,  61–63 showing/hiding elements,  67–68 transforms,  63–67 ,  15–16 validating user input,  190–209 button element,  205–206 button input type,  203–205 checkbox input type,  200–201 content attributes,  206–209 email input type,  198–199 password input control,  197–198 pattern specification,  207–208 placeholder attribute,  208 radio input type,  201–202 range input type,  202–203 read-only controls,  206–207 required controls,  208–209 spell checker,  207 text and textarea input types,  193–195 url input type,  195–197 writing code to interact with UI controls,  22–58 adding/modifying HTML elements,  22–34 implementing graphics with element,  39–58 implementing media controls,  34–39 SVG (Scalable Vector Graphics,  55–58 HTML5 semantic markup,  2–15.  See also HTML5 elements element,  8–9 327

HTML applications, styling

element,  11–12 core structure of an HTML5 page,  3–4 element,  15–16 and elements,  12–13 and elements,  5 element,  7 element,  14 element,  6 element,  13–14 element,  9–10 HTML applications, styling box properties,  244–263 appearance attributes,  244–249 element position,  258–263 graphic effects,  249–258 text properties,  235–242 alignment, 240 bold, 237 color,  236–237 fonts,  238–239 hyphenation,  241–242 italic, 238 spacing, 241 hyphenation, applying to text,  241–242 hyphen property,  241

I identity operators,  113 idle value, app cache staus property,  79 if keyword,  113 iFrames, preventing code injection,  217 if statements,  113–115 images, drawing with element,  52–53 implementation document structure and objects applying styling to HTML5 elements,  60–68 creating and implementing objects and methods,  93–100 creating document structure,  2–19 establishing scope of objects and variables,  86–91 HTML5 APIs,  72–84 graphics, element,  39–58 media controls,  34–39 element,  38–39 element,  35–38 program flow,  112–129 advanced arrays,  122–125 328

arrays,  117–120 callbacks,  156–170 evaluating expressions,  112–116 exception handling,  149–154 iterative control flow,  125–129 raising and handling events,  130–147 special types of arrays,  121–122 web worker process,  172–180 !important keyword, overriding inheritance,  307–308 increment, for loops,  126 indeterminate progress tasks,  14 indexed positions, arrays,  117 indexOf method,  119 inheritance,  99–100, 307–308 inherit value, visibility property,  68 inline display,  68 input controls, validating user input,  190–206 button element,  205–206 button input type,  203–205 checkbox input type,  200–201 email input type,  198–199 password input control,  197–198 radio input type,  201–202 range input type,  202–203 text and textarea input types,  193–195 url input type,  195–197 insertBefore method,  30 inset parameter, box-shadow property,  252, 255 intervals, web worker process,  180 isFinite function, validating data,  216 isNaN function, validating data,  216 italic, applying to text,  238 iterative flow control,  125–129 do...while loops,  128–129 for...in loops,  127 for loops,  126–127 short circuiting loops,  129 while loops,  127–128 iterative program flow,  111

J JavaScript validating user input,  211–217 built-in functions,  216 preventing code injection,  216–217 regular expressions,  211–215 writing code to interact with UI controls,  22–58 adding/modifying HTML elements,  22–34

map method

implementing graphics with element,  39–58 implementing media controls,  34–39 SVG (Scalable Vector Graphics,  55–58 JavaScript alert method,  25 JavaScript messaging framework, Web Worker API and, 176 JavaScript Object Notification (JSON) strings,  76 join method,  119 jQuery, 28 creating dynamic webpages,  161–165 finding elements,  300–304 wiring events,  165–167 jQuery.serialize method, serializing and deserializing binary data,  227–228 JSON data consuming data,  219 serializing and deserializing,  225 JSON (JavaScript Object Notification) strings,  76 justify value, text-align property,  240

K keyboard events,  140–141 keyCode property, keyboard events,  141 keydown events,  140 key method,  73 keypress events,  140 keyup events,  140 keywords break,  115, 129 else, 113 @font-face, 238 if, 113 !important, overriding inheritance,  307–308 leveraging this keyword,  91 new, 117 optional default,  115 self, 177 switch, 115 throw, 153 var, declaring variables,  87

L lastChild property,  31 lastIndexOf method,  119

layout containers, creating document structure,  15–16 layout, content,  266–287 flexible box model,  266–273 grid alignment,  280–285 multi-column layout,  273–276 position, floating, and exclusions,  276–279 regions, grouping, and nesting,  286–287 left property, positioning elements,  61 left value, text-align property,  240 length property arrays,  118–119 storage objects,  73 leveraging, this keyword,  91 lifetime, variables and variable scope,  87–90 limitations, web workers,  179–180 linear-gradient function,  251–252 linear gradients,  51 linear value, transition-timing property,  290 lineCap property,  43 lines, drawing with element,  41–43 lineTo method,  41 lineWidth property,  42 :link pseudo-class,  302 LoadFromStorage method,  74 local scope variables,  87 localStorage object,  72 local Web storage,  72 location services, Geolocation API,  81–84 logical operators,  112 loop attribute, element,  35 loops do...while,  128–129 for,  126–127 for...in, 127 short circuiting,  129 while,  127–128

M manifest file, AppCache API,  77–79 manipulation, document structure and objects applying styling to HTML5 elements,  60–68 creating and implementing objects and methods,  93–100 creating document structure,  2–19 establishing scope of objects and variables,  86–91 HTML5 APIs,  72–84 manual value, hyphen property,  241 map method,  125 329

margins, styling HTML apps

margins, styling HTML apps,  247–249 element,  14 matrix 3-D transformation,  291 max attribute, element,  13 maximumAge property (PositionOptions object),  82 maximum value, wrap-flow property,  277 media controls,  34–39 element,  38–39 element,  35–38 media queries, adjusting UI animation,  292–297 message property, exception objects,  150 methods Abort, 220 addColorStop, 51 addEventListener,  133–134, 146 AFunction, 89 appendChild, 29 arc, 43 beginPath,  41, 47 bezierCurveTo, 43 BFunctionWithParam, 89 clear, 73 closePath, 47 close, WebSocket objects,  161 concat,  118–119 createPattern, 52 createRadialGradient, 51 creating and implementing,  93–100 custom objects,  95–98 inheritance,  99–100 native objects,  94 currentTime, 38 dispatchEvent, 146 document.createElement, 29 drawImage, 52 error, 83 evenNumberCheck,  123, 124 every,  122–123 fill, element,  49–52 fillRect,  49 filter,  124 forEach, 124 Form.Submit, serializing and deserializing binary data,  226–227 getAllResponseHeaders, 220 getCurrentPosition (Geolocation API),  81–83 getElementById,  23, 25, 30, 60 getElementsByClassName,  23, 26 getElementsByTagName,  23, 26

330

getItem, 73 getResponseHeader, 220 indexOf, 119 insertBefore, 30 JavaScript alert,  25 join, 119 jQuery.serialize, serializing and deserializing binary data,  227–228 key, 73 lastIndexOf, 119 lineTo, 41 LoadFromStorage, 74 map, 125 moveTo,  41, 45 multiplyNumbers, 154 MyCallBack, 156 object.create, 99 onerror, Worker objects,  177 onmessage, Worker objects,  176 Open, 220 path, element,  47–48 pause(),  38 play(),  38 pop, 121 postMessage passing parameters,  179 Worker objects,  176 push, 121 quadradicCurveTo, 43 quadraticArc, 45 quadraticCurveTo, 45 querySelector,  23,  27–28 querySelectorAll,  23, 27–28 rect, element,  48–49 reduce, 125 reduceRight, 125 removeChild, 32 removeEventListener,  133–134 removeItem, 73 removeNode, 32 replaceChild, 33 replaceNode, 33 reverse,  119–120 rotate, applying transforms to elements,  64–65 scale, applying transforms to elements,  66 Send, 220 setInterval, 180 setItem, 73 setRequestHeader, 220

overriding inheritance, !important keyword

setTimeout, 180 shift,  121–122 skew, applying transforms to elements,  66 slice, 120 some, 123 sort, 120 splice, 120 stroke, 41 strokeText, 53 success, 83 swapCache, 79 terminate, Worker objects,  176 translate, applying transforms to elements,  65 translateX, 65 translateY, 65 unshift,  121–122 update, 79 volume, 38 watchPosition (Geolocation API),  83–84 WillCallBackWhenDone, 156 WorkWithCanvas, 153 modifying HTML5 elements,  22–34 altering the DOM,  28–34 DOM (Document Object Model),  22–23 selecting items in the DOM,  23–28 month input element,  193 mousedown events,  142 mouseenter events,  142 mouse events,  141–143 mouseleave events,  142 mousemove events,  142 mouseover events,  142 mouseup events,  142 moveTo method,  41, 45 multi-column layout, content,  273–276 multi-dimensional arrays,  117 multiplyNumbers method,  154 MyCallBack function,  156

N named flow,  286 name property, exception objects,  150 native objects, creating and implementing,  94 element,  6 nesting conditional statements,  114 nesting content,  286–287 NETWORK section, AppCache API manifest file,  78

new keyword,  117 none value, hyphen property,  241 null values, exception handling,  154 number input element,  193 number property, exception objects,  150

O object.create method,  99 object inheritance,  99–100 objects creating and implementing,  93–100 custom objects,  95–98 inheritance,  99–100 native objects,  94 scope,  86–91 avoiding use of global namespace,  90–91 leveraging the this keyword,  91 lifetime,  87–90 Obsolete value, app cache status property,  79 offsetX property, mouse events,  142 offsetY property, mouse events,  142 oncached event,  80 onchecking event,  80 ondownloading event,  80 onerror event,  80 onerror method, Worker objects,  177 onmessage event handlers,  160 onmessage method, Worker objects,  176 onnoupdate event,  80 onobsolete event,  80 onprogress event,  80 onreadystatechange event,  219 ontimeout event,  219 onupdateready event,  80 opacity property,  249–250 opacity, styling HTML apps,  249–250 Open method,  220 OPEN value, readyState property,  161 operators conditional, 112 equality, 113 identity, 113 logical, 112 ternary, 116 optional default keyword,  115 OR logical operator,  114 overriding inheritance, !important keyword,  307–308

331

padding, styling HTML apps

P padding, styling HTML apps,  247–249 parameters AJAX call,  164–165 bezierCurveTo method,  46 box-shadow property,  252 drawing arcs,  44 linear-gradient function,  251–252 postMessage method,  179 quadraticCurveTo method,  45–46 rect method,  48 text-shadow property,  256–257 WebSocket constructors,  159 XMLHttpRequest open method,  221 passing parameters, postMessage method,  179 password input control, validating user input,  197–198 path methods, element,  47–48 pattern attribute, validating user input,  207–208 pause() method, element,  38 placeholder attribute, validating user input,  208 play() method, object,  38 polyline, 42 pop method,  121 positionError object,  83 PositionOptions object, properties available,  81 position property,  258–259 poster attribute, element,  35 postMessage method, passing parameters,  179 preventing code injection,  216–217 program flow,  112–129 advanced arrays,  122–125 arrays,  117–120 behavioral, 111 callbacks,  156–170 anonymous functions,  167–169 bidirectional communication with WebSocket API,  157–161 dynamic webpages, jQuery and AJAX,  161–165 this pointer,  169–170 wiring events with jQuery,  165–167 conditional, 111 evaluating expressions,  112–116 if statements,  113–115 switch statements,  115–116 ternary operators,  116 exception handling,  149–154 checking for null values,  154 try...catch...finally constructs,  149–154 iterative, 111 332

iterative flow control,  125–129 raising and handling events,  130–147 addEventListener and removeEventListener methods,  133–134 anonymous functions,  134–135 assignment event handling,  133–134 bubbled events,  136–138 canceling events,  135–136 custom events,  146 declarative event handling,  132 DOM events,  139–146 event objects,  131–132 special types of arrays,  121–122 web worker process,  172–180 configuring timeouts and intervals,  180 using web workers,  178–179 Web Worker API,  172–177 web worker limitations,  179–180 element,  13–14 properties background-image,  250–251 border-color, 245 border-spacing, 246 border-style, 245 border-width, 246 Bottom, 61 box-shadow, 252 clip,  257–258 color, 236 coords, 83 display,  67, 297 DOM elements,  31 exception objects,  150 fillStyle,  50 flex-wrap,  272–273 float,  262–263 flow-from,  286 flow-into,  286 font-family, 238 font-size, 239 font typeface,  238 font-weight, 237 grid-columns, 282 grid-column-span, 285 grid-rows, 282 grid-row-span, 285 hyphen, 241 keyboard events,  141 Left, 61 length

scale 3-D transformation

arrays,  118–119 storage objects,  73 lineCap, 43 lineWidth, 42 mouse events,  142 multi-column, 274 opacity,  249–250 position,  258–259 PositionOptions object,  81 readyState,  160–161 Right, 61 success, AJAX calls,  164 text-align, 240 textAlign, 54 text-indent, 240 text-shadow,  252, 256–257 timestamp, 83 Top, 61 transform, 63 transition-delay, 290 transition-duration, 290 transition-property, 290 transition-timing, 290 element,  35, 38 visibility,  68, 297 visibility CSS,  68 window.navigator, 81 wrap-flow,  276–279 wrap-margin, 278 XMLHttpRequest object,  220 z-index, 261 pseudo-classes, finding elements,  301–304 pseudo-elements, finding elements,  301–304 push method,  121

quadraticArc method,  45 quadraticCurveTo method,  43, 45 querySelectorAll method,  23, 27–28 querySelector method,  23, 27–28 queues (arrays),  121

raising an error (throwing an exception),  153 raising events,  130–147 addEventListener and removeEventListener methods,  133–134 anonymous functions,  134–135 assignment event handling,  133–134 bubbled events,  136–138 canceling events,  135–136 custom events,  146 declarative event handling,  132 DOM events,  139–146 event objects,  131–132 range input type, validating user input,  202–203 readonly attribute, validating user input,  206–207 readyState property,  160–161, 220 rect method, element,  48–49 reduce method,  125 reduceRight method,  125 referencing elements, CSS files,  306–307 regions, content layout,  286–287 regular expressions, validating user input,  211–215 relative positioning,  61, 259–261 removeChild method,  32 removeEventListener method,  133–134 removeItem method,  73 removeNode method,  32 replaceChild method,  33 replaceNode method,  33 :required pseudo-class,  302 required controls, validating user input,  208–209 reset input element,  193, 204 responseBody property (XMLHttpRequest object),  220 Response property (XMLHttpRequest object),  220 responseText property (XMLHttpRequest object),  220 responseType property (XMLHttpRequest object),  220 responseXML property (XMLHttpRequest object),  220 reverse method,  119–120 RGB function, color property,  236 Right property, positioning elements,  61 right value, text-align property,  240 rotate 3-D transformation,  291 rotate method, applying transforms to elements,  64–65 row-reverse, flexbox content,  269

R

S

radial gradients,  51 radio input type, validating user input,  201–202 radius parameter, drawing arcs,  44

sandbox attribute values,  217 Scalable Vector Graphics (SVG),  55–58 scale 3-D transformation,  291

Q

333

scale method, applying transforms to elements

scale method, applying transforms to elements,  66 scope, objects and variables,  86–91 avoiding use of global namespace,  90–91 leveraging the this keyword,  91 lifetime,  87–90 screen readers, creating document structure,  17–19 screenX property, mouse events,  142 screenY property, mouse events,  142 search engine optimization (SEO),  16–17 search engines, creating document structure,  16–17 element,  9–10 secure data consuming data,  218–223 serializing, deserializing, and transmitting data,  224–228 binary data,  225–228 JSON data,  225 XMLHttpRequest object,  224–225 validating user input, HTML5 elements,  190–209 content attributes,  206–209 input controls,  190–206 validating user input, JavaScript,  211–217 built-in functions,  216 preventing code injection,  216–217 regular expressions,  211–215 selectors (CSS3), finding elements,  300–304 choosing the correct selector to reference an element, 301 defining element, style, and attribute selectors,  300–301 selector syntax, jQuery,  165 self keyword,  177 Send function, WebSocket API,  160 Send method,  220 SEO (search engine optimization),  16–17 serializing data,  224–228 binary data,  225–228 JSON data,  225 sessionStorage object,  72 session Web storage,  72 setInterval method,  180 setItem method,  73 setRequestHeader method,  220 setTimeout method,  180 shadow effects, styling HTML apps,  252–257 shapes fill method,  49–52 rect method,  48–49 shiftKey property, keyboard events,  141 shift method,  121–122 334

short circuiting loops,  129 showing elements,  67–68 size, styling HTML box properties,  244–245 sizing arrays,  117 skew method, applying transforms to elements,  66 slice method,  120 some method,  123 sort method,  120 spacing, applying to text,  241 special arrays,  121–122 special characters, regular expressions,  212–213 spellcheck attribute, validating user input,  207 splice method,  120 spread parameter, box-shadow property,  252-254 src attribute, element,  35 stacked case statements,  116 stacks (arrays),  121 startAngle parameter, drawing arcs,  44 start value, wrap-flow property,  277 statements if,  113–115 switch,  115–116 static data,  189 static layout,  258 status property AppCace API,  79 XMLHttpRequest object,  220 statusText property (XMLHttpRequest object),  220 stroke method,  41 strokeText method,  53 structure, creating document structure,  2–19 HTML5 semantic markup,  2–15 layout containers,  15–16 optimizing for search engines,  16–17 screen readers,  17–19 structuring CSS files, CSS selectors,  305–308 style selectors (CSS3),  300–301 styles, flexible boxes,  267 styling HTML, 60-68 box properties,  244–263 appearance attributes,  244–249 element position,  258–263 graphic effects,  249–258 text properties,  235–242 alignment, 240 bold, 237 color,  236–237 fonts,  238–239 hyphenation,  241–242 italic, 238

user input, validating

spacing, 241 submit input element,  193, 204 subworkers, web worker process,  180 success method,  83 success property, AJAX calls,  164 SVG (Scalable Vector Graphics),  55–58 swapCache method,  79 switch keyword,  115 switch statements,  115–116

T element,  15–16 tel input element,  193 terminate method, Worker objects,  176 ternary operators,  116 text-align property,  54, 240 textarea input control, validating user input,  193–195 text, drawing with element,  53–55 text drop shadows.  See shadow effect text-indent property,  240 text input control, validating user input,  193–195 text-shadow property,  252, 256–257 text, styling HTML properties,  235–242 alignment, 240 bold, 237 color,  236–237 fonts,  238–239 hyphenation,  241–242 italic, 238 spacing, 241 this keyword, leveraging,  91 this pointer,  169–170 throwing an exception (raising an error),  153 throw keyword,  153 time input element,  193 timeout property PositionOptions object,  81 XMLHttpRequest object,  220 timeouts, web worker process,  180 timestamp property,  83 top property, positioning elements,  61 transformations, creating an animated UI,  291–292 transform property,  63 transforms, applying to HTML5 elements,  63–67 transition-delay property,  290 transition-duration property,  290 transition-property property,  290

transitions (CSS), animating objects,  289–290 transition-timing-property, 290 translate 3-D transformation,  291 translate method, applying transforms to elements,  65 translateX method,  65 translateY method,  65 transmitting data,  224–228 transparency, styling HTML apps,  249–250 try block (try...catch...finally constructs),  150 try…catch block (try...catch...finally constructs),  150 try...catch...finally constructs, exception handling,  149–154 two-dimensional arrays,  118

U UI controls, writing code to interact with,  22–58 adding/modifying HTML elements,  22–34 implementing graphics with element,  39– 58 implementing media controls,  34–39 SVG (Scalable Vector Graphics,  55–58 Uncached value, app cache status property,  79 unshift method,  121–122 update method,  79 UpdateReady value, app cache status property,  79 url input type, validating user input,  195–197 user input, validating HTML5 elements,  190–209 button element,  205–206 button input type,  203–205 checkbox input type,  200–201 content attributes,  206–209 email input type,  198–199 password input control,  197–198 pattern specification,  207–208 placeholder attribute,  208 radio input type,  201–202 range input type,  202–203 read-only controls,  206–207 required controls,  208–209 spell checker,  207 text and textarea input types,  193–195 url input type,  195–197 JavaScript,  211–217 built-in functions,  216 preventing code injection,  216–217 regular expressions,  211–215

335

validating user input

V validating user input HTML5 elements,  190–209 button element,  205–206 button input type,  203–205 checkbox input type,  200–201 content attributes,  206–209 email input type,  198–199 password input control,  197–198 pattern specification,  207–208 placeholder attribute,  208 radio input type,  201–202 range input type,  202–203 read-only controls,  206–207 required controls,  208–209 spell checker,  207 text and textarea input types,  193–195 url input type,  195–197 JavaScript,  211–217 built-in functions,  216 preventing code injection,  216–217 regular expressions,  211–215 value attribute, element,  13 values hyphen property,  241 status property (AppCace API),  79 text-align property,  240 visibility property,  68 variables, scope,  86–91 avoiding use of global namespace,  90–91 leveraging the this keyword,  91 lifetime,  87–90 var keyword, declaring variables,  87 element,  35–38 visibility property,  68, 297 visible value, visibility property,  68 :visited pseudo-class,  302 volume method, element,  38 v-shadow parameter box-shadow property,  252 text-shadow property,  256

Web Storage API,  72–77 Web Worker API,  172–177 web worker process, creating,  172–180 configuring timeouts and intervals,  180 using web workers,  178–179 Web Worker API,  172–177 web worker limitations,  179–180 web workers,  178–179 week input element,  193 while loops,  127–128 width parameter rect method,  48 element,  35 WillCallBackWhenDone function,  156 window.navigator property,  81 wiring events, jQuery,  165–167 withCredentials property (XMLHttpRequest object),  220 Worker objects,  176 WorkWithCanvas method,  153 wrap-flow property,  276–279 wrap-margin property,  278 writing code, interaction with UI controls,  22–58 adding/modifying HTML elements,  22–34 implementing graphics with element,  39–58 implementing media controls,  34–39 SVG (Scalable Vector Graphics,  55–58

X XML data, consuming data,  219 XMLHttpRequest object consuming data,  219–223 properties, 220 transmitting data,  224–225 x, y parameter, rect method,  48

Z z-index property,  261

W watchPosition method, Geolocation API,  83–84 web services, consuming JSON and XML data,  219 WebSocket API, bidirectional communication,  157–161 WebSocket constructors, parameters,  159 336

About the author RICK DE LORME is a Senior Software Architect and Implementer. Since graduating with an

Information Systems degree from St. Francis Xavier University in Antigonish, Nova Scotia, 14 years ago, he has worked on large, multi-factorial projects ranging from call center applications, to postal industry applications, and more recently, including applications in health care. Rick has leveraged all the elements of the Microsoft development stack to deliver cohesive solutions. The addition of HTML5 and CSS3 has provided Rick (and all developers) with more tools to make the end-user experience richer and to simplify development of web applications. Throughout Rick’s career, he has authored several books related to debugging of .NET applications, design and development of custom web components, .NET deployment, and general .NET development, since the first pre-release of the .NET Framework. When Rick is not working, he enjoys spending time with his family and supporting his kids in their various activities. Rick currently resides in Ontario, Canada, with his wife, three children, and two dogs.

This page intentionally left blank

Free ebooks From technical overviews to drilldowns on special topics, get free ebooks from Microsoft Press at: www.microsoftvirtualacademy.com/ebooks Download your free ebooks in PDF, EPUB, and/or Mobi for Kindle formats. Look for other great resources at Microsoft Virtual Academy, where you can learn new skills and help advance your career with free Microsoft training delivered by experts.

Microsoft Press

Now that you’ve read the book... Tell us what you think! Was it useful? Did it teach you what you wanted to learn? Was there room for improvement? Let us know at http://aka.ms/tellpress Your feedback goes directly to the staff at Microsoft Press, and we read every one of your responses. Thanks in advance!

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF