Escolar Documentos
Profissional Documentos
Cultura Documentos
JavaScript Specialist
Academic Student Guide
JavaScript Specialist
Developers
Brian Danks, Elliot Cherner, Ashley Craft and Susan M. Lane
Contributors
Lisa Pease
Editor
Susan M. Lane
Project Manager/Publisher
Tina Strong
Trademarks
Certification Partners is a trademark of Certification Partners, LLC. All product names and services identified throughout
this book are trademarks or registered trademarks of their respective companies. They are used throughout this book in
editorial fashion only. No such use, or the use of any trade name, is intended to convey endorsement or other affiliation
with the book. Copyrights of any screen captures in this book are the property of the software's manufacturer.
Disclaimer
Certification Partners, LLC, makes a genuine attempt to ensure the accuracy and quality of the content described
herein; however, Certification Partners makes no warranty, express or implied, with respect to the quality, reliability,
accuracy, or freedom from error of this document or the products it describes. Certification Partners makes no
representation or warranty with respect to the contents hereof and specifically disclaims any implied warranties of
fitness for any particular purpose. Certification Partners disclaims all liability for any direct, indirect, incidental or
consequential, special or exemplary damages resulting from the use of the information in this document or from the
use of any products described in this document. Mention of any product or organization does not constitute an
endorsement by Certification Partners of that product or corporation. Data used in examples and labs is intended to be
fictional even if actual data is used or accessed. Any resemblance to, or use of real persons or organizations should be
treated as entirely coincidental. Certification Partners makes every effort to ensure the accuracy of URLs referenced in
all its material, but cannot guarantee that all URLs will be available throughout the life of a course. When this course
was published, all URLs were checked for accuracy and completeness. However, due to the ever-changing nature of the
Internet, some URLs may no longer be available or may have been redirected.
Copyright Information
This training manual is copyrighted and all rights are reserved by Certification Partners, LLC. No part of this publication
may be reproduced, transmitted, stored in a retrieval system, modified, or translated into any language or computer
language, in any form or by any means, electronic, mechanical, magnetic, optical, chemical, manual or otherwise
without written permission of Certification Partners, 1230 W. Washington Street, Suite 111, Tempe, AZ 85281.
Copyright 2011 by
Certification Partners, LLC
All Rights Reserved
ISBN: 0-7423-2928-3
vi
Version 1.01
vii
Table of Contents
Course Description...................................................................................................................... xiv
Courseware .................................................................................................................................. xv
Course Objectives...................................................................................................................... xviii
Classroom Setup ....................................................................................................................... xviii
System Requirements ............................................................................................................... xviii
Conventions and Graphics Used in This Book .............................................................................. xxi
Lesson 1: Introduction to JavaScript........................................................................................... 1-1
Pre-Assessment Questions ................................................................................................................ 1-2
Introduction to Scripting ................................................................................................................... 1-3
JavaScript Characteristics................................................................................................................. 1-3
JavaScript and Common Programming Concepts............................................................................... 1-5
JavaScript Flavors and Versions........................................................................................................ 1-6
Server-Side vs. Client-Side Applications............................................................................................. 1-9
Annotating Your Code with Comments ............................................................................................ 1-13
Case Study...................................................................................................................................... 1-16
Lesson 1 Review .............................................................................................................................. 1-18
Lesson 2: Working with Variables and Data in JavaScript ............................................................ 2-1
Pre-Assessment Questions ................................................................................................................ 2-2
Using JavaScript to Communicate with the User ............................................................................... 2-3
Using Data More Than Once: Variables ........................................................................................... 2-12
JavaScript Expressions ................................................................................................................... 2-17
JavaScript Operators....................................................................................................................... 2-18
Inline Scripting, Simple User Events, and Basic Event Handlers ...................................................... 2-26
JavaScript Keywords and Reserved Words ....................................................................................... 2-28
Case Study...................................................................................................................................... 2-30
Lesson 2 Review .............................................................................................................................. 2-33
Lesson 3: Functions, Methods and Events in JavaScript.............................................................. 3-1
Pre-Assessment Questions ................................................................................................................ 3-2
Introduction to Functions.................................................................................................................. 3-2
Defining a Function........................................................................................................................... 3-3
Calling a Function............................................................................................................................. 3-6
User Events and JavaScript Event Handlers .................................................................................... 3-16
Methods as Functions ..................................................................................................................... 3-17
Case Study...................................................................................................................................... 3-21
Lesson 3 Review .............................................................................................................................. 3-23
Lesson 4: Controlling Program Flow in JavaScript ....................................................................... 4-1
Pre-Assessment Questions ................................................................................................................ 4-2
Controlling Decisional Program Flow ................................................................................................. 4-3
The if...else Statement ....................................................................................................................... 4-3
The while Statement.......................................................................................................................... 4-8
The do...while Statement ................................................................................................................. 4-11
The for Statement............................................................................................................................ 4-13
The break Statement ....................................................................................................................... 4-16
The continue Statement ................................................................................................................... 4-18
The switch Statement ...................................................................................................................... 4-21
Case Study...................................................................................................................................... 4-27
Lesson 4 Review .............................................................................................................................. 4-30
Lesson 5: The JavaScript Document Object Model (DOM) ............................................................ 5-1
Pre-Assessment Questions ................................................................................................................ 5-2
The JavaScript Document Object Model (DOM).................................................................................. 5-3
The window Object............................................................................................................................ 5-4
The document Object ....................................................................................................................... 5-16
The with Statement ......................................................................................................................... 5-19
The image Object............................................................................................................................. 5-24
The history Object ........................................................................................................................... 5-31
Version 1.01
viii
Version 1.01
ix
List of Labs
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
1-1:
2-1:
2-2:
2-3:
2-4:
2-5:
2-6:
2-7:
3-1:
3-2:
Version 1.01
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
Lab
3-3: Calling a function from within another function in JavaScript ............................................... 3-12
4-1: Using if statements ................................................................................................................. 4-6
4-2: Using a while statement.......................................................................................................... 4-9
4-3: Using a for statement ............................................................................................................ 4-15
4-4: Nesting if and break statements inside a while loop............................................................... 4-17
4-5: Using a continue statement ................................................................................................... 4-20
4-6: Using a switch statement ...................................................................................................... 4-24
5-1: Launching a new window with the open() method .................................................................... 5-8
5-2: Writing content to new windows............................................................................................ 5-11
5-3: Changing status bar text....................................................................................................... 5-15
5-4: Assigning properties to a remote documentdynamically......................................................... 5-20
5-5: Preloading and swapping images to create an active link ....................................................... 5-27
5-6: Identifying browser properties with the navigator object ........................................................ 5-33
5-7: Redirecting to a page based on browser type ......................................................................... 5-37
6-1: Using String object formatting methods ................................................................................... 6-6
6-2: Applying String methods to text............................................................................................. 6-16
6-3: Creating an Array object ....................................................................................................... 6-27
6-4: Accessing and using dates with the Date object ..................................................................... 6-31
6-5: Creating an onscreen clock ................................................................................................... 6-36
6-6: Using the Math object to generate a random quotation .......................................................... 6-40
7-1: Working with text boxes, check boxes and buttons................................................................ 7-13
7-2: Working with radio buttons................................................................................................... 7-17
7-3: Working with selection lists................................................................................................... 7-21
7-4: Working with a multiple-selection list box ............................................................................. 7-25
7-5: Conducting form validation ................................................................................................... 7-27
8-1: Performing client-side browser detection ................................................................................. 8-9
8-2: Locking the browser with malicious code............................................................................... 8-16
8-3: Setting, viewing and clearing a cookie with JavaScript........................................................... 8-36
8-4: Setting passwords with cookies ............................................................................................. 8-40
9-1: Creating a custom object....................................................................................................... 9-16
10-1: Redirecting a page based on user input with getElementById............................................... 10-5
10-2: Changing the DOM using getElementsByName .................................................................. 10-10
10-3: Getting, setting and removing X/HTML attributes ............................................................. 10-16
11-1: Loading a JavaScript library and running a library script .................................................... 11-8
11-2: Using CSS and JavaScript to create a basic slideshow....................................................... 11-10
11-3: Loading, testing and editing a library plug-in .................................................................... 11-13
12-1: Using AJAX to dynamically edit the DOM with button clicks ............................................. 12-10
12-2: Using AJAX and libraries to create tooltips........................................................................ 12-18
13-1: Installing and debugging with the Mozilla Firebug add-on ................................................... 13-9
13-2: Troubleshooting a logic error in JavaScript........................................................................ 13-15
List of Figures
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Version 1.01
xi
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Version 1.01
xii
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Version 1.01
xiii
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
Figure
12-1: Diagram of AJAX interactions among JavaScript, XML and X/HTML............................... 12-6
12-2: Database table consisting of fields (columns) and records (rows) ................................... 12-14
13-1: Internet Explorer error alert for syntax error ................................................................... 13-5
13-2: Disabling debugger in Internet Explorer .......................................................................... 13-6
13-3: Accessing JavaScript Console in Chrome's Tools menu ................................................... 13-7
13-4: Chrome error alert for syntax error ................................................................................. 13-7
13-5: Firefox rendering page without errors.............................................................................. 13-8
13-6: Mozilla Firefox home page Add-ons menu.................................................................... 13-9
13-7: Firebug Add-ons home page .......................................................................................... 13-10
13-8: Firefox security alert warning about add-on installation ................................................ 13-10
13-9: Opening Firebug debugger in Firefox............................................................................. 13-11
13-10: Firebug error alert box ................................................................................................ 13-11
13-11: Debugged JavaScript rendered as expected ................................................................. 13-12
List of Tables
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Table
Version 1.01
xiv
Course Description
The JavaScript Specialist course focuses on the fundamental concepts of the JavaScript language. This
course will empower you with the skills to design client-side, platform-independent solutions that greatly
increase the value of your Web site by providing interactivity and interest. You will learn how to use
JavaScript to communicate with users, modify the Document Object Model (DOM), control program flow,
validate forms, animate images, create cookies, change XHTML on the fly, and communicate with
databases.
The first part of this course focuses on essential JavaScript principles and techniques, including common
programming practices, working with variables and data, and using functions, methods and events.
Next, you will learn intermediate JavaScript programming techniques that will build upon your knowledge
and present solutions for more functional and exciting Web pages. These include using control structures
for program flow, manipulating the JavaScript DOM, using language objects, creating expressions, and
creating and using custom objects.
You will then apply JavaScript programming techniques to develop interactive forms, change XHTML
dynamically, create and use cookies, and explore security issues related to JavaScript. You will also learn
about extensions to the JavaScript language, including JavaScript code libraries and advanced interactive
Web applications with AJAX. Finally, you will learn about tools and techniques for debugging and
troubleshooting your JavaScript code.
By the end of this course, you will understand and use the most popular applications of JavaScript.
Guided, step-by-step labs provide opportunities to practice new skills. You can challenge yourself and
review your skills after each lesson in the Lesson Summary and Lesson Review sections. Additional skill
reinforcement is provided in Optional Labs, Lesson Quizzes and a Course Assessment that are available
from your instructor. This course also offers Case Studies and pre-lab scenarios for class discussion
about real-world skills applications.
This coursebook comes with supplemental materials that include the lab files used in class. To practice
the skills presented in class or to perform any labs that were not completed, refer to the Classroom Setup
section for information about system requirements and using the lab files.
Note: Most JavaScript code is widely provided as freeware and is not subject to copyright restrictions.
Some code used in this course's examples and labs for instructional purposes was provided in part or in
whole as freeware, and was not necessarily written in full by CIW developers.
The CIW JavaScript Specialist course prepares students to take the CIW JavaScript Specialist certification
exam (1D0-635).
Series
JavaScript Specialist is the first course in the CIW Web Development series:
JavaScript Specialist
Perl Specialist
Prerequisites
No prior knowledge of the JavaScript programming language is necessary. However, students should be
familiar with using the Internet, developing Web pages and configuring networks to a level consistent with
that found in the CIW Web Foundations series. Knowledge of X/HTML code development is required.
Version 1.01
xv
Certification
The CIW JavaScript Fundamentals course prepares candidates to take the CIW JavaScript Specialist exam
(1D0-635), which if passed earns the individual the CIW JavaScript Specialist certification.
Candidates who also earn the CIW Perl Specialist certification and the CIW Database Design Specialist
certification earn the advanced CIW Web Development Professional certification in addition.
The CIW Web Development Professional certification program prepares students to excel in a career in
Web and IT technologies. This advanced certification validates skills in the essential combination of a
front-end scripting language, a back-end programming language and the ability to integrate them with a
database.
To earn the CIW Web Development Professional certification, students must pass three certification
exams:
Target audience
The CIW JavaScript Specialist course is for individuals who want to understand how to fully use the
features of the JavaScript programming language in Web application development. These specialized Web
language skills can help you begin or grow a career in advanced Web development and technologies:
Web developers
Software developers
Application programmers
Client/server developers
Desktop publishers
Courseware
This coursebook was developed for instructor-led training and will assist you during class. Along with
comprehensive instructional text and objectives checklists, this coursebook provides easy-to-follow
hands-on labs and a glossary of course-specific terms. It also provides Internet addresses needed to
complete some labs, although due to the constantly changing nature of the Internet, some addresses may
no longer be valid.
The student coursebook is organized in the following manner:
Version 1.01
xvi
course title
table of contents
list of labs
list of figures
list of tables
lessons
lesson objectives
pre-assessment questions
narrative text
exam objective callouts
graphics
tables and figures
warnings
tech notes
supplemental movie clips
labs
exam objective callouts
graphics
tables and figures
warnings
tech notes
case study
lesson summary
lesson review
appendixes
glossary
index
supplemental files
When you return to your home or office, you will find this coursebook to be a valuable resource for
reviewing labs and applying the skills you have learned. Each lesson concludes with questions that review
the material. Lesson review questions are provided as a study resource only and in no way guarantee a
passing score on the CIW JavaScript Specialist certification exam.
Coursebook versions
The CIW Web Development courseware is designed for various classroom environments: academic,
learning center and corporate. These coursebooks are available in both instructor and student versions.
Student versions are available for both the academic environment and the learning center/corporate
environment. Check your book to verify which version you have.
Instructor (Academic, Learning Center and Corporate) Example syllabi for 10-week and 16week instruction periods are included with the instructor supplemental files available on CIW Online.
Learning centers can teach this series at an accelerated pace; consult the implementation table that
can be found on CIW Online. The supplemental files also include an appendix listing the CIW
JavaScript Specialist certification exam objectives and locations of corresponding material in the
coursebook. The instructor version of this book includes Instructor Notes in the margin, which
provide additional tips and commentary for the instructor to supplement course narrative. Margin
callouts also direct instructors to material that relates directly to specified CIW JavaScript Specialist
objectives. The instructor book and supplemental files contain all answers to Optional Labs, Lesson
Quizzes and the Course Assessment. The supplemental files also include handout versions of all
Optional Labs, Lesson Quizzes and the Course Assessment, which the instructor can print and assign
during class or as homework. Lesson Quizzes and Course Assessments are provided as study and
Version 1.01
xvii
course-grading resources only; success on these materials in no way guarantees a passing score on
the CIW JavaScript Specialist certification exam.
Student (Academic) The student book and supplemental files include Pre-Assessment and Lesson
Review questions for each lesson. However, the student book does not provide answers to these
questions. It also does not include any Optional Labs, Quizzes or the Course Assessment. Students
can obtain these elements and answers only from the instructor. The student supplemental materials
include appendixes and files used to perform many of the labs in the coursebook. The supplemental
files also include an appendix listing the CIW JavaScript Specialist certification exam objectives and
locations of corresponding material in the coursebook. Lesson Quizzes and Course Assessments are
provided as study and course-grading resources only; success on these materials in no way
guarantees a passing score on the CIW JavaScript Specialist certification exam.
Online resources
You can visit CIW Online at http://education.certification-partners.com/ciw/ to access supplemental
course materials and to get help in preparing for the CIW JavaScript Specialist certification exam.
Version 1.01
xviii
Timed practice exams that simulate the high-stakes testing environment and help predict actual
performance on CIW certification exams.
A feedback review mode that allows you to check answers while taking the practice exam and gain
valuable feedback that relates each question to a CIW exam objective and a lesson in the Official CIW
Courseware.
Exam results that report on your mastery of each CIW exam objective.
Personalized performance reports and study plans to track individual progress and view overall class
trends.
Course Objectives
After completing this class, you will be able to:
Address security issues relevant to JavaScript, and define and use cookies.
Classroom Setup
Your instructor has probably set up the classroom computers based on the system requirements listed in
the following sections. Most software configurations on your computer are identical to those on your
instructor's computer. However, your instructor may use additional software to demonstrate network
interaction or related technologies.
System Requirements
This section lists the hardware, software and connectivity requirements to implement this course.
Version 1.01
xix
Hardware
The following table summarizes the hardware requirements for all courses in the CIW program. Each
classroom should be equipped with enough personal computers to accommodate each student and the
instructor with his or her own system.
Note: The CIW hardware requirements are similar to the minimum system requirements for Microsoft
Windows XP implementation except that CIW requires increased hard disk space (20 GB) and RAM (1 GB).
Processor
L2 cache
256 KB
Hard disk
20 GB
RAM
1 GB
CD-ROM
32X
Sound card/speakers
Video adapter
At least 4 MB
Monitor
Network hubs
Router*
* Router only required for some security and networking courses. Please review the connectivity or
network requirements for each course to determine if a router is required. Must meet universal CIW
hardware requirements.
Software
The recommended software configurations for computers used to complete the labs in this book are as
follows.
To be installed before class:
A standard text editor, such as Notepad, Notepad++ or WordPad (included with Microsoft Windows).
Connectivity
Internet connectivity is required for this course. You will experience optimal performance with a dedicated
Internet connection (e.g., a cable/DSL modem or a T1 line).
Version 1.01
xx
Version 1.01
xxi
Technology terms defined in the margins are indicated in bold type the first time
they appear in the text. However, not every word in bold type is a term requiring
definition.
Lab Text
Text that you enter during a lab appears in italic bold type. Names of components
that you access or change in a lab appear in bold type.
Notations
Notations or comments regarding screenshots, labs or other text are indicated in italic
type.
Program Code or
Commands
Text used in program code or operating system commands appears in the Lucida
Sans Typewriter font.
This graphic indicates a line of code that is completed on the following line.
Version 1.01
xxii
Version 1.01