JAVASCRIPT FOR PROGRAMMERS DEITEL DEVELOPER SERIES Paul J. Deitel Deitel & Associates, Inc. Harvey M. Deitel Deitel & Associates, Inc. PRENTICE HALL Upper Saddle River, NJ Boston Indianapolis San Francisco New York Toronto Montreal London Munich Paris Madrid Capetown Sydney Tokyo Singapore Mexico City
Contents Preface Before Уои Begin xv xxiii i Introduction I 1.1 Introduction 2 1.2 History of the Internet and World Wide Web 3 1.3 World Wide Web Consortium (W3C) 5 1.4 Web 2.0 5 1.5 Key Software Trend: Object Technology 7 1.6 JavaScript: Object-Based Scripting for the Web 8 1.7 Browser Portability 9 1.8 Web Resources 9 2 Introduction to XHTML 10 2.1 Introduction 11 2.2 Editing XHTML 12 2.3 First XHTML Example 12 2.4 W3C XHTML Validation Service 15 2.5 Headings 15 2.6 Linking 16 2.7 Images 19 2.8 Special Characters and Horizontal Rules 22 2.9 Lists 24 2.10 Tables 27 2.11 Forms 31 2.12 Internal Linking 38 2.13 meta Elements 40 2.14 Web Resources 42 3 Cascading Style Sheets (CSS) 43 3.1 Introduction 44 3.2 Inline Styles 45 3.3 Embedded Style Sheets 46 3.4 Conflicting Styles 49
x Contents 3.5 Linking External Style Sheets 3.6 Positioning Elements 3.7 Backgrounds 3.8 Element Dimensions 3.9 Box Model and Text Flow 3.10 Media Types 3.11 Building a CSS Drop-Down Menu 3.12 User Style Sheets 3.13 CSS3 3.14 Web Resources 4 JavaScript: Introduction to Scripting 4.1 Introduction 4.2 Simple Program: Displaying a Line of Text in a Web Page 4.3 Modifying Our First Program 4.4 Obtaining User Input with prompt Dialogs 4.4.1 Dynamic Welcome Page 4.4.2 Adding Integers 4.5 Data Types in JavaScript 4.6 Arithmetic 4.7 Decision Making: Equality and Relational Operators 4.8 Web Resources 5 JavaScript: Control Statements I 5.1 Introduction 5.2 Control Statements 5.3 i f Selection Statement 5.4 if...else Selection Statement 5.5 while Repetition Statement 5.6 Counter-Controlled Repetition 5.7 Formulating Algorithms: Sentinel-Controlled Repetition 5.8 Formulating Algorithms: Nested Control Statements 5.9 Assignment Operators 5.10 Increment and Decrement Operators 5.11 Web Resources 6 JavaScript: Control Statements 11 6.1 Introduction 6.2 Essentials of Counter-Controlled Repetition 6.3 for Repetition Statement 6.4 Examples Using the for Statement 6.5 switch Multiple-Selection Statement 6.6 do...whi 1 e Repetition Statement 6.7 break and continue Statements
6.8 Labeled break and conti nue Statements 6.9 Logical Operators 7 JavaScript: Functions 7.1 Introduction 7.2 Functions 7.3 Programmer-Defined Functions 7.4 Function Definitions 7.5 Random Number Generation 7.6 Example: Game of Chance 7.7 Another Example: Random Image Generator 7.8 Scope Rules 7.9 JavaScript Global Functions 7.10 Recursion 7.11 Recursion vs. Iteration 8 JavaScript: Arrays 8.1 Introduction 8.2 Arrays 8.3 Declaring and Allocating Arrays 8.4 Examples Using Arrays 8.5 Random Image Generator Using Arrays 8.6 References and Reference Parameters 8.7 Passing Arrays to Functions 8.8 Sorting Arrays 8.9 Multidimensional Arrays 8.10 Building an Online Quiz 9 JavaScript: Objects 9.1 Introduction 9.2 Introduction to Object Technology 9.3 Math Object 9.4 String Object 9.4.1 Fundamentals of Characters and Strings 9.4.2 Methods of the Stri ng Object 9.4.3 Character-Processing Methods 9.4.4 Searching Methods 9.4.5 Splitting Strings and Obtaining Substrings 9.4.6 XHTML Markup Methods 9.5 Date Object 9.6 Boolean and Number Objects 9.7 document Object 9.8 window Object 9.9 Using Cookies
xii Contents 9.10 Multipage HTML and JavaScript Application 9.11 Using JSON to Represent Objects 10 Document Object Model (DOM): Objects and Collections 10.1 Introduction 10.2 Modeling a Document: DOM Nodes and Trees 10.3 Traversing and Modifying a DOM Tree 10.4 DOM Collections 10.5 Dynamic Styles 10.6 Summary of the DOM Objects and Collections I I JavaScript: Events 11.1 Introduction 11.2 Registering Event Handlers 11.3 Event onload 11.4 Event onmousemove, the event Object and this 11.5 Rollovers with onmouseover and onmouseout 11.6 Form Processing with onfocus and onblur 11.7 Form Processing with onsubmit and onreset 11.8 Event Bubbling 11.9 More Events 11.10 Web Resources 12 XMLandRSS 12.1 Introduction 12.2 XML Basics 12.3 Structuring Data 12.4 XML Namespaces 12.5 Document Type Definitions (DTDs) 12.6 W3C XML Schema Documents 12.7 XML Vocabularies 12.7.1 MathML 12.7.2 Other Markup Languages 12.8 Extensible Stylesheet Language and XSL Transformations 12.9 Document Object Model (DOM) 12.10 RSS 12.11 Web Resources 1 3 Ajax-Enabled Rich Internet Applications 13.1 Introduction 13.2 Traditional Web Applications vs. Ajax Applications
13.3 Rich Internet Applications (RIAs) with Ajax 13.4 History of Ajax 13.5 "Raw" Ajax Example Using the XMLHttpRequest Object 13.6 Using XML and the DOM 13.7 Creating a Full-Scale Ajax-Enabled Application 13.8 Dojo Toolkit 13.9 Web Resources A XHTML Special Characters XHTML Colors С JavaScript Operator Precedence Char C. 1 Operator Precedence Chart Index