Learning JavaScript By Shelley Powers Publisher: O'Reilly Pub Date: October 01, 2006 ISBN: 0-596-52746-2 Pages: 304 Table of Contents | Index As web browsers have become more capable and standards compliant, JavaScript has grown in prominence JavaScript lets designers add sparkle and life to web pages, while more complex JavaScript has led to the rise of Ajax the latest rage in web development that allows developers to create powerful and more responsive applications in the browser window Learning JavaScript introduces this powerful scripting language to web designers and developers in easy-to-understand terms Using the latest examples from modern browser development practices, this book teaches you how to integrate the language with the browser environment, and how to practice proper coding techniques for standardscompliant web sites By the end of the book, you'll be able to use all of the JavaScript language and many of the object models provided by web browsers, and you'll even be able to create a basic Ajax application Learning JavaScript By Shelley Powers Publisher: O'Reilly Pub Date: October 01, 2006 ISBN: 0-596-52746-2 Pages: 304 Table of Contents | Index Copyright Preface Chapter 1 Introduction and First Looks Section 1.1 Twisted History: Specs and Implementations Section 1.2 Cross-Browser Incompatibility and Other Common JavaScript Myths Section 1.3 What You Can Do with JavaScript Section 1.4 First Look at JavaScript: "Hello World!" Section 1.5 The JavaScript Sandbox Section 1.6 Accessibility and JavaScript Best Practices Chapter 2 JavaScript Data Types and Variables Section 2.1 Identifying Variables Section 2.2 Scope Section 2.3 Simple Types Section 2.4 Constants: Named but Not Variables Section 2.5 Questions Chapter 3 Operators and Statements Section 3.1 Format of a JavaScript Statement Section 3.2 Simple Statements Section 3.3 Conditional Statements and Program Flow Section 3.4 The Conditional Operators Section 3.5 The Logical Operators Section 3.6 Advanced Statements: The Loops Section 3.7 Questions Chapter 4 The JavaScript Objects Section 4.1 The Object Constructor Section 4.2 The Number Object Section 4.3 The String Object Section 4.4 Regular Expressions and RegExp Section 4.5 Purposeful Objects: Date and Math Section 4.6 JavaScript Arrays Section 4.7 Associative Arrays: The Arrays That Aren't Section 4.8 Questions Chapter 5 Functions Section 5.1 Defining a Function: Let Me Count the Ways Section 5.2 Callback Functions Section 5.3 Functions and Recursion Section 5.4 Nested Functions, Function Closure, and Memory Leaks Section 5.5 Function As Object Section 5.6 Questions Chapter 6 Catching Events Section 6.1 The Event Handler at DOM Level 0 Section 6.2 Questions Chapter 7 Forms and JiT Validation Section 7.1 Accessing the Form Section 7.2 Attaching Events to Forms: Different Approaches Section 7.3 Selection Section 7.4 Radio Buttons and Checkboxes Section 7.5 Input Fields and JiT Regular Expressions Section 7.6 Questions Chapter 8 The Sandbox and Beyond: Cookies, Connectivity, and Piracy Section 8.1 The Sandbox Section 8.2 All About Cookies Section 8.3 Alternative Storage Techniques Section 8.4 Cross-Site Scripting (XSS) Section 8.5 Questions Chapter 9 The Basic Browser Objects Section 9.1 BOM at a Glance Section 9.2 The window Object Section 9.3 Frames and Location Section 9.4 history, screen, and navigator Section 9.5 The all Collection, Inner/Outer HTML and Text, and Old and New Documents Section 9.6 Something Old, Something New Section 9.7 Questions Chapter 10 DOM: The Document Object Model Section 10.1 A Tale of Two Interfaces Section 10.2 The DOM and Compliant Browsers Section 10.3 The DOM HTML API Section 10.4 Understanding the DOM: The Core API Section 10.5 The DOM Core Document Object Section 10.6 Element and Access in Context Section 10.7 Modifying the Tree Section 10.8 Questions Chapter 11 Creating Custom JavaScript Objects Section 11.1 The JavaScript Object and Prototyping Section 11.2 Creating Your Own Custom JavaScript Objects Section 11.3 Object Detection, Encapsulation, and Cross-Browser Objects Section 11.4 Chaining Constructors and JS Inheritance Section 11.5 One-Off Objects Section 11.6 Advanced Error-Handling Techniques (try, throw, catch) Section 11.7 What's New in JavaScript Section 11.8 Questions Chapter 12 Building Dynamic Web Pages: Adding Style to Your Script Section 12.1 DHTML: JavaScript, CSS, and DOM Section 12.2 Fonts and Text Section 12.3 Position and Movement Section 12.4 Size and Clipping Section 12.5 Display, Visibility, and Opacity Section 12.6 Questions Chapter 13 Moving Outside the Page with Ajax Section 13.1 Ajax: It's Not Only Code Section 13.2 How Ajax Works Section 13.3 Hello Ajax World! Section 13.4 The Ajax Object: XMLHttpRequest and IE's ActiveX Objects Section 13.5 Working with XMLor Not Section 13.6 Google Maps Section 13.7 Questions Chapter 14 Good News: Juicy Libraries! Amazing Web Services! Fun APIs! Section 14.1 Before Jumping In, A Word of Caution Section 14.2 Working with Prototype Section 14.3 Script.aculo.us: More Than the Sum of Its Periods Section 14.4 Sabre's Rico Section 14.5 Dojo Section 14.6 The Yahoo! UI Section 14.7 MochiKit Section 14.8 Questions Appendix 1 Answers Section A.1 Chapter 2 Section A.2 Chapter 3 Section A.3 Chapter 4 Section A.4 Chapter 5 Section A.5 Chapter 6 Section A.6 Chapter 7 Section A.7 Chapter 8 Section A.8 Chapter 9 Section A.9 Chapter 10 Section A.10 Chapter 11 Section A.11 Chapter 12 Section A.12 Chapter 13 Section A.13 Chapter 14 Colophon Index Copyright © 2007 O'Reilly Media, Inc All rights reserved Printed in the United States of America Published by O'Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472 O'Reilly books may be purchased for educational, business, or sales promotional use Online editions are also available for most titles (safari.oreilly.com) For more information, contact our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com Editor: Simon St Laurent Production Editor: Rachel Monaghan Copy Editor: Mary Anne Weeks Mayo Proofreader: Rachel Monaghan Indexer: Johnna VanHoose Dinse Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrators: Robert Romano and Jessamyn Read Printing History: October 2006: First Edition Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered trademarks of O'Reilly Media, Inc Learning JavaScript, the image of a baby rhino, and related trade dress are trademarks of O'Reilly Media, Inc Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in this book, and O'Reilly Media, Inc was aware of a trademark claim, the designations have been printed in caps or initial caps While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein Preface JavaScript was originally intended to be a scripting interface between a web page loaded in the browser client (Netscape Navigator at the time), and the application on the server Since its introduction in 1995, JavaScript has become a key component of web development, and has found uses elsewhere as well This book covers the JavaScript language, from its most primitive data types that have been around since the beginnings of the language, to its most complex features, including those involved with Ajax and DHTML By the end of the book, you will have the basics you need to work with even the most sophisticated libraries and web applications Audience Readers of this book should be familiar with web page technology, including CSS and HTML/XHTML You may well have seen some JavaScript in that work Previous programming experience isn't required, though some sections may require extra review if you have no previous exposure to programming This book should help: Anyone who wants, or needs, to integrate JavaScript into his own personal web site or sites Anyone who uses a content-management tool, such as a weblogging tool, and wants to better understand the scripting components incorporated into her tool templates Web developers who seek to integrate JavaScript and some of the DHTML/Ajax features into their web sites Web service developers who want to develop for a new market of clients Teachers who use web technologies as either the focus or a component of their courses Web page designers who wish to better understand how their designs can be enlivened with interactive or animated effects Anyone interested in web technologies Assumptions and Approach As stated earlier, this book assumes you have experience with (X)HTML and CSS, as well as a general understanding of how web applications work Programming experience isn't necessary, but the book covers all aspects of JavaScript, some of which are relatively sophisticated Though the heavier pieces are few, you will need to understand JavaScript enough to work with the newer Ajax libraries The book is broken into four sections: Chapters 1 through 3 provide an introduction to the structure of a JavaScript application, including the simple data types supported in the language, as well as the basic statements and control structures These establish a baseline of understanding of the language for the sections that follow Chapters 4 through 8 introduce the main JavaScript objects, including the all-important function, script access for web-page forms, event handling, scripting security, and working with cookies Combined, these topics comprise the core of JavaScript, and with these chapters, you can validate form elements, set and retrieve cookies, capture and provide functionality for events, and even create JavaScript libraries The functionality covered in these chapters has been basic to JavaScript for 10 years, and will remain so for at least another 10 Chapters 9 through 11 delve into the more sophisticated aspects of web-page development These chapters cover the Browser Object Model and the newer Document Object Model, and show how you can create your own custom objects Understanding these models is essential if you wish to create new windows, or individually access, modify, or even dynamically create any page element In addition, with custom objects, you can then move beyond the capabilities that are prebuilt into either language or browser Chapters 12 through 14 get into the advanced uses of JavaScript, including DHTML, Ajax, and some of the many wonderful new libraries that support both Chapter 1, Introduction and First Looks Introduces JavaScript and provides a quick first look at a small web-page application This chapter also covers some issues associated with the use of JavaScript, including the many tools that are available, as well as issues of security and accessibility Chapter 2, JavaScript Data Types and Variables Provides an overview of the basic data types in JavaScript, as well as an overview of language variables, identifiers, and the structure of a JavaScript statement introduction libraries location 2nd Math methods properties navigator Number Object one-off Prototype library prototyping RegExp screen String window one-off objects opacity 2nd operators = (assignment) arithmetic assignment with operation binary bitwise equality logical precedence property relational ternary unary overflow Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z] parseFloat function parseInt function password field permalink, Ajax position property precedence of operators private properties, custom objects program flow, conditional statements and properties bottom Event object event object innerHTML left Math object nodes, DOM position prototype right String object style top visiblity property operator Prototype library $( ) function $F function $H function $R function helper functions objects prototype property prototyping objects public properties, custom objects Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z] queues, arrays, FIFO Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z] radio buttons introduction JiT validation recursive functions RegExp object exec method text method regular expressions 2nd JiT validation relational operators removeEventListener reserved words resizeBy method resizeTo method returns, functions Rico library right property Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z] same origin security policy sandbox scope scope, variables screen object script tag attributes script.aculo.us library security Ajax same origin policy select element 2nd 3rd setTimeout method SO (Shared Objects), cookies and statements arithmetic assignment conditional program flow and semicolons switch string data type string data types backslash string literals String object style property styles, fonts subtraction (-) operator switch statement Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z] tags, script ternary operator test method RegExp object text properties text field textarea field this keyword, object properties and timers top property try statement Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z] unary operators user-defined functions Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z] validation, form fields var keyword variables global identifiers keywords Unicode local naming guidelines prototype effect scope visibility property Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z] while loop whitespace window object custom windows dialog windows open method resizeBy method resizeTo method windows cross-window communication custom dialog windows modifying Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z] XML XMLHttpRequest existence of methods XSS (cross-site scripting) Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z] Yahoo! UI Library Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z] z-index .. .Learning JavaScript By Shelley Powers Publisher: O'Reilly Pub Date: October 01, 2006 ISBN: 0-596-52746-2 Pages: 304 Table of Contents | Index... Cross-Browser Incompatibility and Other Common JavaScript Myths Section 1.3 What You Can Do with JavaScript Section 1.4 First Look at JavaScript: "Hello World!" Section 1.5 The JavaScript Sandbox Section 1.6 Accessibility and JavaScript Best Practices... Printing History: October 2006: First Edition Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered trademarks of O'Reilly Media, Inc Learning JavaScript, the image of a baby rhino, and related