Your hands-on, step-by-step guide to the fundamentals of JavaScript development. Teach yourself how to program with JavaScript -- one step at time. Ideal for developers with fundamental programming skills, this practical tutorial provides the clear guidance and hands-on examples you need to create or customize interactive Web applications using core JavaScript features and techniques. Discover how to: Write and deploy JavaScript code with Microsoft® Visual Studio® 2010, the Eclipse IDE, or text editors Work with JavaScript syntax and data types Use the Document Object Model to retrieve, create, and change HTML elements Create rollover images and slideshow effects Validate and provide feedback for user input on Web forms Manipulate CSS styles and respond to browser events Develop interactive Web applications using AJAX Help speed development with JavaScript frameworks such as jQuery Your Step by Step digital content includes: All the books practice files -- ready to download and put to work. Fully searchable online edition of this book -- with unlimited access on the Web. The print version of this book includes free digital content online. To download, please visit O'Reilly's web site, search for the title of this book to find its catalog page, and click on the link below the cover image (Examples, Companion Content, or Practice Files). Note that while we provide as much of the media content as we are able via free download, we are sometimes limited by licensing restrictions. Please direct any questions or concerns to booktech@oreilly.com. Contents at a Glance 6 Table of Contents 8 Acknowledgements 18 Introducing JavaScript Step by Step, Second Edition 20 Getting Help 23 Part I. JavaWhat? The Where, Why, 24 Chapter 1. JavaScript Is More Than You Might Think 26 A Brief History of JavaScript 26 Enter Internet Explorer 3.0 27 And Then Came ECMAScript 27 So Many Standards... 28 The DOM 28 What’s in a JavaScript Program? 29 JavaScript Placement on Your Webpage 30 What JavaScript Can Do 33 What JavaScript Can’t Do 33 JavaScript Can’t Be Forced on a Client 33 JavaScript Can’t Guarantee Data Security 34 JavaScript Can’t Cross Domains 34 JavaScript Doesn’t Do Servers 35 Tips for Using JavaScript 35 Where JavaScript Fits 37 Which Browsers Should the Site Support? 38 What’s New in ECMAScript Version 5? 39 New Array Methods 39 New Controls on Object Properties 39 New JSON Object 39 Changes to the Date Object 40 A New Strict Mode 40 Browser Support 40 Exercises 40 Chapter 2. Developing in JavaScript 42 JavaScript Development Options 42 Configuring Your Environment 43 Writing JavaScript with Visual Studio 2010 43 Your First Web (and JavaScript) Project with Visual Studio 2010 47 Using External JavaScript Files with Visual Studio 2010 51 Writing JavaScript with Eclipse 56 Your First Web (and JavaScript) Project with Eclipse 56 Using External JavaScript Files with Eclipse 61 Writing JavaScript Without an IDE 64 Your First Web (and JavaScript) Project with Notepad 64 Using External JavaScript Files Without an IDE 67 Debugging JavaScript 69 Exercises 70 Chapter 3. JavaScript Syntax and Statements 72 A Bit of Housekeeping 72 Case Sensitivity 72 White Space 73 Comments 74 Semicolons 74 Line Breaks 76 Placing JavaScript Correctly 76 JavaScript Statements 77 What’s in a Statement? 77 The Two Types of JavaScript Statements 78 Reserved Words in JavaScript 78 A Quick Look at Functions 79 JavaScript’s New Strict Mode 82 Exercises 83 Working with Variables and Data Types 84 Data Types in JavaScript 84 Working with Numbers 85 Working with Strings 89 Booleans 94 Null 94 Undefined 94 Objects 95 Arrays 96 Defining and Using Variables 96 Declaring Variables 97 Variable Types 97 Variable Scope 98 The Date Object 105 Using the RegExp Object 114 The Syntax of Regular Expressions 115 References and Garbage Collection 119 Learning About Type Conversions 120 Number Conversions 120 String Conversions 121 Boolean Conversions 121 Exercises 121 Using Operators and Expressions 122 Meet the Operators 122 Additive Operators 122 Multiplicative Operators 123 Bitwise Operators 124 Equality Operators 125 Relational Operators 127 The in Operator 128 The instanceof Operator 128 Unary Operators 129 Incrementing and Decrementing 129 Converting to a Number with the Plus Sign 130 Creating a Negative Number with the Minus Sign 130 Negating with bitwise not and logical not 130 Using the delete Operator 131 Returning Variable Types with the typeof Operator 134 The void Operator 135 Assignment Operators 136 The Comma Operator 137 Exercises 138 Part II. Applying JavaScript 140 Chapter 6. Controlling Flow with Conditionals and Loops 142 If (and How) 142 Syntax for if Statements 142 The prompt() Function in Internet Explorer 144 Compound Conditions 147 Using else if and else Statements 149 Working with Ternary Conditionals 154 Testing with switch 155 Looping with while 157 The while Statement 157 The do...while Statement 158 Using for Loops 160 The for Loop 160 The for...in Loop 163 The for each...in Loop 164 Validating Forms with Conditionals 166 Exercises 168 Chapter 7. Working with Functions 170 What’s in a Function? 170 Function Arguments 171 Variable Scoping Revisited 173 Return Values 174 More on Calling Functions 175 Anonymous/Unnamed Functions (Function Literals) 177 Closures 178 Methods 178 A Look at Dialog Functions 179 Exercises 184 Chapter 8. Objects in JavaScript 186 Object-Oriented Development 186 Objects 186 Properties 187 Methods 187 Classes 187 Creating Objects 190 Adding Properties to Objects 191 Adding Methods to Objects 194 Finding Out More About Arrays 194 The length Property 195 Array Methods 195 Taking Advantage of Built-in Objects 201 The Global Object 201 Exercises 203 Chapter 9. The Browser Object Model 204 Introducing the Browser 204 The Browser Hierarchy 204 Events 205 A Sense of Self 206 Getting Information About the Screen 208 Using the navigator Object 210 The location Object 214 The history Object 221 Exercises 222 Part III. Integrating JavaScript into Design 224 Chapter 10. The Document Object Model 226 The Document Object Model Defined 226 DOM Level 0: The Legacy DOM 227 DOM Levels 1 and 2 227 The DOM as a Tree 228 Working with Nodes 229 Retrieving Elements 229 Retrieving Elements by ID 229 Retrieving by Tag Name 233 HTML Collections 235 Working with Siblings 235 Working with Attributes 236 Viewing Attributes 236 Setting Attributes 239 Creating Elements 240 Adding Text 240 Adding an Element and Setting an ID 242 Deleting Elements 242 Exercises 244 Chapter 11. JavaScript Events and the Browser 246 Understanding Window Events 246 The Event Models 246 A Generic Event Handler 250 Detecting Visitor Information 251 A Brief Look at the userAgent Property 252 Feature Testing 253 Keeping JavaScript Away from Older Browsers 254 Other navigator Properties and Methods 255 Opening, Closing, and Resizing Windows 256 Window Opening Best Practices 257 Opening Tabs: No JavaScript Necessary? 258 Resizing and Moving Windows 259 Timers 259 Exercises 261 Chapter 12. Creating and Consuming Cookies 262 Understanding Cookies 262 Creating Cookies with JavaScript 263 Looking at a Simple Cookie 264 Setting a Cookie’s Expiration Date 264 Setting the Cookie Path 268 Setting the Cookie Domain 269 Working with Secure Cookies 270 Reading Cookies with JavaScript 271 Removing Cookies 273 Exercises 274 Chapter 13. Working with Images in JavaScript 276 Working with Image Rollovers 276 A Simple Rollover 276 Modern Rollovers 278 Preloading Images 285 Working with Slideshows 286 Creating a Slideshow 286 Moving Backward 289 Working with Image Maps 293 Exercises 297 Chapter 14. Using JavaScript with Web Forms 298 JavaScript and Web Forms 298 Obtaining Form Data 301 Working with Form Information 302 Working with Select Boxes 302 Working with Check Boxes 307 Working with Radio Buttons 310 Prevalidating Form Data 312 Hacking JavaScript Validation 312 Validating a Text Field 316 Exercises 318 Chapter 15. JavaScript and CSS 320 What Is CSS? 320 Using Properties and Selectors 321 Applying CSS 322 The Relationship Between JavaScript and CSS 323 Setting Element Styles by ID 323 Setting Element Styles by Type 327 Setting CSS Classes with JavaScript 329 Retrieving Element Styles with JavaScript 330 Modifying Style Sheets with JavaScript 331 Exercises 334 Chapter 16. JavaScript Error Handling 336 Introducing Two Ways to Handle Errors 336 Using try/catch 336 And Finally... 344 Using the onerror Event 345 Attaching onerror to the window Object 345 Ignoring Errors 347 Attaching onerror to the image Object 348 Exercises 350 Part IV. AJAX and Server-Side Integration 352 Chapter 17. JavaScript and XML 354 Using XML with JavaScript 354 Looking at an Example XML Document 354 Loading an XML Document with JavaScript 355 Working with XML Data from Excel 2007 362 A Preview of Things to Come 363 Exercises 363 Chapter 18. JavaScript Applications 364 Components of JavaScript Applications 364 The Big Three: Display, Behavior, Data 364 JavaScript and Web Interfaces 365 Chapter 19. A Touch of AJAX 368 Introduction to AJAX 368 The XMLHttpRequest Object 369 Instantiating the XMLHttpRequest Object 369 Sending an AJAX Request 371 Processing an AJAX Response 373 Processing XML Responses 377 Working with JSON 378 Processing Headers 379 Using the POST Method 380 Case Study: Live Searching and Updating 382 Exercises 388 Chapter 20. A Bit Deeper into AJAX 390 Creating an HTML Table with XML and CSS 390 Styling the Table with CSS 394 Changing Style Attributes with JavaScript 394 Creating a Dynamic Drop-Down Box 397 Accepting Input from the User and AJAX 402 Exercises 403 Part V. jQuery 404 Chapter 21. An Introduction to JavaScript Libraries and Frameworks 406 Understanding Programming Libraries 406 Defining Your Own JavaScript Library 406 Looking at Popular JavaScript Libraries and Frameworks 408 jQuery 408 Yahoo! User Interface 408 MooTools 409 Other Libraries 409 Exercises 409 Chapter 22. An Introduction to jQuery 410 jQuery Primer 410 Using jQuery 410 The Two jQuery Downloads 410 Including jQuery 411 Basic jQuery Syntax 411 Connecting jQuery to the Load Event 412 Using Selectors 414 Selecting Elements by ID 414 Selecting Elements by Class 414 Selecting Elements by Type 415 Selecting Elements by Hierarchy 415 Selecting Elements by Position 416 Selecting Elements by Attribute 419 Selecting Form Elements 420 More Selectors 420 Functions 420 Traversing the DOM 421 Working with Attributes 426 Changing Text and HTML 426 Inserting Elements 427 Callback Functions 427 Events 428 Binding and Unbinding 428 Mouse Events and Hover 430 Many More Event Handlers 431 AJAX and jQuery 432 AJAX Errors and Timeouts 435 Sending Data to the Server 435 Other Important Options 436 More jQuery 436 Exercises 437 Chapter 23. jQuery Effects and Plug-Ins 438 Core Features for Enhancing Usability 438 Native Effects 438 jQuery UI 443 Using jQuery UI 443 Drag and Drop 444 Accordion 446 More jQuery UI 450 Exercises 451 Appendix 452 Chapter 1 452 Chapter 2 453 Chapter 3 454 Chapter 4 454 Chapter 5 455 Chapter 6 456 Chapter 7 457 Chapter 8 458 Chapter 9 459 Chapter 10 460 Chapter 11 462 Chapter 12 463 Chapter 13 464 Chapter 14 464 Chapter 15 468 Chapter 16 469 Chapter 17 471 Chapter 18 476 Chapter 19 476 Chapter 20 477 Chapter 21 478 Chapter 22 478 Chapter 23 479 Index 482 JavaWhat : the where, why and how of JavaScript. JavaScript is more than you might think ; Developing in JavaScript ; JavaScript syntax and statements ; Working with variables and data types ; Using operators and expressions Applying JavaScript. Controlling flow with conditionals and loops ; working with functions ; Objects in JavaScript ; The browser object model Integrating JavaScript into design. The document object model ; JavaScript events and the browser ; Creating and consuming cookies ; Working with images in JavaScript ; Using JavaScript with web forms ; JavaScript and CSS ; JavaScript error handling AJAX and beyond. JavaScript and XML ; JavaScript applications ; A touch of AJAX ; A bit deeper into AJAX jQuery. An introduction to JavaScript libraries and frameworks ; An introduction to jQuery ; jQuery effects and plug-ins.