Preface
After the fourth edition of JavaScript: The Definitive Guide was published, the Document Object Modelâthe fundamental API for client-side JavaScript⢠programmingâbecame widely, if not completely, implemented in web browsers. This meant that web developers had a stable, mature language (JavaScript 1.5) and a common API for manipulating web pages on the client. Several years of stability followed.
But things have started to get interesting again. Developers are now using JavaScript to script HTTP, manipulate XML data, and even draw dynamic graphics in a web browser. Many JavaScript developers have also started to write longer programs and use more sophisticated programming techniques, such as closures and namespaces. This fifth edition has been fully revised for the new world of Ajax and Web 2.0 technologies.
Whatâs New in the Fifth Edition
In Part I, the chapter on functions (Chapter 8) has been expanded, with particular emphasis on nested functions and closures. The material on defining your own classes has also been expanded and moved to a chapter of its own (Chapter 9). Chapter 10 is another new chapter that covers namespaces, which are essential for writing modular, reusable code. Finally, Chapter 12 demonstrates how to use JavaScript to actually script Java; it shows how to embed a JavaScript interpreter within a Java 6 application and how to use JavaScript to create Java objects and invoke methods on those objects.
In Part II, the coverage of the legacy (Level 0) Document Object Model has been merged with the coverage of the W3C standard DOM. Because the DOM is now universally implemented, there is no need for two separate chapters on manipulating documents. The biggest change in Part II, however, is the amount of new material:
Chapter 19, has updated coverage of cookies and new coverage of other client-side persistence techniques.
Chapter 20, explains how to make scripted HTTP requests using the powerful XMLHttpRequest object, which is the enabling technology for Ajax-style web applications.
Chapter 21, demonstrates how to use JavaScript to create, load, parse, transform, query, serialize, and extract information from XML documents. It also introduces the E4X extension to the core JavaScript language.
Chapter 22, explains JavaScriptâs graphics capabilities. It covers simple image rollovers and animations but also explains advanced scripted graphics using the cutting-edge
<canvas>
tag. It also demonstrates other ways to create dynamic, scripted client-side graphics using SVG, VML, the Flash plug-in, and the Java plug-in.Chapter 23, has added coverage of the Flash plug-in to its coverage of the Java plug-in. It now explains how to script Flash movies as well as Java applets.
Part III, the reference section for the core JavaScript API, is little changed from the previous edition, because that API has remained stable. If you have used the fourth edition, youâll find this part of the book comfortingly familiar.
The big change to the reference material is that the documentation
of the DOM API, which was previously segregated into a section of its
own, has been fully integrated into Part IV,
the client-side JavaScript reference section. Now there is only one
client-side reference section to consult. No more looking up the
Document object in one reference section and then looking up the
HTMLDocument object in the other. Reference material for DOM interfaces
that were never (widely) implemented in web browsers has simply been
cut. The NodeIterator interface, for example, is simply not available in
web browsers, and it no longer clutters up this book. The focus has also
been moved away from the awkwardly formal interfaces defined by the DOM
specification and onto the JavaScript objects that actually implement
those interfaces. So, for example, getComputedStyle( )
is now documented as a
method of the Window object, where youâd expect it, rather than as a
method of the AbstractView interface. There is no reason that
client-side JavaScript programmers should care about AbstractView, and
it has simply been removed from the reference. All these changes result
in a simpler, easier-to-use client-side reference section.
Using This Book
Chapter 1 provides an introduction to JavaScript. The rest of the book is in four parts. Part I, which immediately follows this chapter, documents the core JavaScript language. Chapter 2 through 6 cover some bland but necessary reading; these chapters cover the basic information you need to understand when learning a new programming language:
Chapter 2, explains the basic structure of the language.
Chapter 3, documents the datatypes supported by JavaScript.
Chapter 4, covers variables, variable scope, and related topics.
Chapter 5, explains expressions in JavaScript and documents each operator supported by JavaScript. Because JavaScript syntax is modeled on Java, which is, in turn, modeled on C and C++, experienced C, C++, or Java programmers can skim much of this chapter.
Chapter 6, describes the syntax and usage of each of the JavaScript statements. Again, experienced C, C++, and Java programmers can skim some, but not all, of this chapter.
The next six chapters of Part I become more interesting. They still cover the core of the JavaScript language, but they document parts of the language that will not be familiar to you even if you already know C or Java. These chapters must be studied carefully if you want to really understand JavaScript:
Chapter 7, explains objects and arrays in JavaScript.
Chapter 8, documents how functions are defined, invoked, and manipulated in JavaScript. It also includes advanced material on closures.
Chapter 9, covers OO programming in JavaScript, explaining how to define constructor functions for new classes of objects and how JavaScriptâs prototype-based inheritance works. This chapter also demonstrates how to simulate traditional class-based OO idioms in JavaScript.
Chapter 10, shows how JavaScript objects define namespaces and explains programming practices that can protect your modules of JavaScript code from namespace collisions.
Chapter 11, explains how to use regular expressions in JavaScript to perform pattern-matching and search-and-replace operations.
Chapter 12, demonstrates how to embed a JavaScript interpreter within a Java application and explains how JavaScript programs running within such an application can script Java objects. This chapter is of interest only to Java programmers.
Part II explains JavaScript in web browsers. The first six chapters cover the core features of client-side JavaScript:
Chapter 13, explains the integration of JavaScript with web browsers. It discusses the web browser as a programming environment and explains the various ways in which JavaScript is integrated into web pages for execution on the client side.
Chapter 14, documents the central object of client-side JavaScriptâthe Window objectâand explains how you can use this object to control web browser windows.
Chapter 15, covers the Document object and explains how JavaScript can script the content displayed within a web browser window. This is the most important chapter in Part II.
Chapter 16, explains how JavaScript and CSS stylesheets interact. It demonstrates how JavaScript can manipulate the style, appearance, and position of elements within an HTML document to produce visual effects known as DHTML.
Chapter 17, covers JavaScript events and event handlers, which are central to all JavaScript programs that interact with the user.
Chapter 18, explains how JavaScript can work with HTML forms and form elements. This is logically an extension of Chapter 15, but the topic is substantial enough that it deserves its own chapter.
These first six chapters of Part II are followed by five more that cover advanced topics in client-side JavaScript.
Chapter 19, covers client-side persistence: the ability of scripts to store data on the userâs computer for later retrieval. This chapter explains how to script HTTP cookies for persistence and also how to achieve persistence using proprietary features of Internet Explorer and the Flash plug-in.
Chapter 20, shows how JavaScript can script the HTTP protocol, sending requests to and receiving responses from web servers using the XMLHttpRequest object. This capability is the cornerstone of a web application architecture known as Ajax.
Chapter 21, demonstrates how to use JavaScript to create, load, parse, transform, query, serialize, and extract information from XML documents.
Chapter 22, explains JavaScriptâs graphics capabilities. It covers simple image rollovers and animations, as well as advanced graphics techniques using Scalable Vector Graphics (SVG), Vector Markup Language (VML), the
<canvas>
tag, the Flash plug-in, and the Java plug-in.Chapter 23, explains how you can use JavaScript to communicate with and control Java applets and Flash movies. It also covers how you can do the reverseâinvoke JavaScript code from Java applets and Flash movies.
Parts III and IV contain reference material covering core JavaScript and client-side JavaScript, respectively. These parts document relevant objects, methods, and properties alphabetically.
Conventions Used in This Book
The following formatting conventions are used in this book:
- Bold
Is occasionally used to refer to particular keys on a computer keyboard or to portions of a user interface, such as the Back button or the Options menu.
- Italic
Is used for emphasis and to indicate the first use of a term. Italic is also used for email addresses, web sites, FTP sites, file and directory names, and newsgroups. Finally, italic is used in this book for the names of Java classes, to help keep Java class names distinct from JavaScript names.
-
Constant width
Is used in all JavaScript code and HTML text listings, and generally for anything that you would type literally when programming.
-
Constant width bold
Is used to indicate command-line text that should be typed by the user.
-
Constant width italic
Is used for the names of function arguments, and generally as a placeholder to indicate an item that should be replaced with an actual value in your program.
Using Code Examples
This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless youâre reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from OâReilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your productâs documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: "JavaScript: The Definitive Guide, by David Flanagan. Copyright 2006 OâReilly Media, Inc., 978-0-596-10199-2.â
If you feel your use of code examples falls outside fair use of the permission given above, feel free to contact us at permissions@oreilly.com.
Safari® Enabled
When you see a Safari® Enabled icon on the cover of your favorite technology book, that means the book is available online through the OâReilly Network Safari Bookshelf.
Safari offers a solution thatâs better than e-books. Itâs a virtual library that lets you easily search thousands of top tech books, cut and paste code samples, download chapters, and find quick answers when you need the most accurate, current information. Try it for free at http://safari.oreilly.com.
How to Contact Us
Please address comments and questions concerning this book to the publisher:
OâReilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)
We have a web page for this book that lists errata, examples, or any additional information. You can access this page at:
http://www.oreilly.com/catalog/jscript5
You can also download the examples from the authorâs web site:
http://www.davidflanagan.com/javascript5
To comment or ask technical questions about this book, send email to:
For more information about our books, conferences, Resource Centers, and the OâReilly Network, see our web site at:
Acknowledgments
Brendan Eich of the Mozilla organization is the originator and chief innovator of JavaScript. I, and many JavaScript developers, owe Brendan a tremendous debt of gratitude for developing JavaScript and for taking the time out of his crazy schedule to answer our questions and even solicit our input. Besides patiently answering my many questions, Brendan also read and provided very helpful comments on the first and third editions of this book.
This book has been blessed with top-notch technical reviewers whose comments have gone a long way toward making it a stronger and more accurate book. Aristotle Pagaltzis (http://plasmasturm.org) reviewed the new material on functions and the new chapters on classes and namespaces in this edition. He gave my code a particularly careful review and had many useful comments. Douglas Crockford (http://www.crockford.com) reviewed the new material on functions and classes. Norris Boyd, creator of the Rhino JavaScript interpreter, reviewed the chapter on embedding JavaScript in Java applications. Peter-Paul Koch (http://www.quirksmode.org), Christian Heilmann (http://www.wait-till-i.com), and Ken Cooper reviewed the Ajax-related chapters of the book. Ken was kind enough to pitch in at the end and help out with new client-side reference material. Todd Ditchendorf (http://www.ditchnet.org) and Geoff Stearns (http://blog.deconcept.com) reviewed the chapter on scripted client-side graphics. Todd was kind enough to find and isolate a bug for me, and Geoff helped me understand Flash and ActionScript. Finally, Sanders Kleinfeld reviewed the entire book with remarkable attention to detail. His suggestions and technical corrections have make the book clearer and more precise. My sincere thanks to each of them for their careful reviews. Any errors that remain are, of course, my own.
I am also grateful to the reviewers of the fourth edition. Waldemar Horwat at Netscape reviewed the new material on JavaScript 1.5. The new material on the W3C DOM was reviewed by Philippe Le Hegaret of the W3C and by Peter-Paul Koch, Dylan Schiemann, and Jeff Yates. Although he was not a reviewer, Joseph Kesselman of IBM Research was very helpful in answering my questions about the W3C DOM.
The third edition of the book was reviewed by Brendan Eich, Waldemar Horwat, and Vidur Apparao at Netscape; Herman Venter at Microsoft; and two independent JavaScript developers, Jay Hodges and Angelo Sirigos. Dan Shafer did some preliminary work on the third edition. Although his material was not used in this edition, his ideas and general outline were quite helpful. Norris Boyd and Scott Furman at Netscape also provided useful information for this edition, and Vidur Apparao of Netscape and Scott Issacs of Microsoft each took the time to talk to me about the forthcoming Document Object Model standard. Finally, Dr. Tankred Hirschmann provided challenging insights into the intricacies of JavaScript 1.2.
The second edition benefited greatly from the help and comments of Nick Thompson and Richard Yaker of Netscape; Dr. Shon Katzenberger, Larry Sullivan, and Dave C. Mitchell at Microsoft; and Lynn Rollins of R&B Communications. The first edition was reviewed by Neil Berkman of Bay Networks and by Andrew Schulman and Terry Allen of OâReilly.
This book also gains strength from the diversity of editors it has had. Deb Cameron is the editor of this edition and has given the book a thorough edit and much-needed freshening, with special emphasis on removing outdated material. Paula Ferguson edited the third and fourth editions. Frank Willison edited the second edition, and Andrew Schulman edited the first.
Finally, my thanks, as always and for so many reasons, to Christie.
Get JavaScript: The Definitive Guide, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.