The links below display the source code for the 124 numbered examples in the 5th edition of my book JavaScript: The Definitive Guide. Cut-and-paste the code in order to try it out yourself and use it in your own programs.
See the "O'Reilly Policy on Re-Use of Code Examples from Books" for details on how you may and may not use these examples. In most cases, it suffices to simply provide suitable attribution, in the source code and documentation of your program, with a comment like the following:
// This code is from the book JavaScript: The Definitive Guide, 5th Edition, // by David Flanagan. Copyright 2006 O'Reilly Media, Inc. (ISBN #0596101996)
Please note that the examples are not production code. They are provided "as-is" and come with no warranty of any kind.
You can download the entire bundle of examples from the O'Reilly website.
| Chapter 1: Introduction to JavaScript | |
|---|---|
| 1-1 | A simple JavaScript program |
| 1-2 | An HTML button with a JavaScript event handler defined |
| 1-3 | Computing loan payments with JavaScript |
| Chapter 3: Datatypes and Values | |
| 3-1 | Copying, passing, and comparing by value |
| 3-2 | Copying, passing, and comparing by reference |
| 3-3 | References themselves are passed by value |
| 3-4 | Are strings compared by value or by reference? |
| Chapter 8: Functions | |
| 8-1 | Defining JavaScript functions |
| 8-2 | Using functions as data |
| 8-3 | Object utility functions |
| 8-4 | Array utility functions |
| 8-5 | Utility functions for functions |
| 8-6 | Private properties with closures |
| 8-7 | Breakpoints using closures |
| Chapter 9: Classes, Constructors, and Prototypes | |
| 9-1 | A circle class |
| 9-2 | A complex number class |
| 9-3 | Subclassing a JavaScript class |
| 9-4 | Borrowing methods from one class for use by another |
| 9-5 | Mixin classes with generic methods for borrowing |
| 9-6 | Enhanced typeof testing |
| 9-7 | Testing whether an object borrows the methods of a class |
| 9-8 | Testing whether an object provides methods |
| 9-9 | Testing for array-like objects |
| 9-10 | A utility function for defining classes |
| 9-11 | Using the defineClass() method |
| Chapter 10: Modules and Namespaces | |
| 10-1 | Creating a namespace based on a domain name |
| 10-2 | A complex number class as a module |
| 10-3 | A module of shapes classes |
| 10-4 | Defining a private namespace with a closure |
| 10-5 | A module of module-related utilities |
| Chapter 12: Scripting Java | |
| 12-1 | A Java program for running JavaScript scripts |
| 12-2 | A Java configuration file utility that interprets JavaScript expressions |
| 12-3 | Implementing a Java interface with JavaScript code. |
| Chapter 14: Scripting Browser Windows | |
| 14-1 | Extracting arguments from a URL |
| 14-2 | Portably querying window geometry |
| 14-3 | Determining browser vendor and version |
| 14-4 | Creating and manipulating windows |
| 14-5 | Using the confirm() method |
| 14-6 | A tasteful status line animation |
| 14-7 | A navigation bar |
| Chapter 15: Scripting Documents | |
| 15-1 | Listing all anchors |
| 15-2 | Traversing the nodes of a document |
| 15-3 | Obtaining all text beneath a DOM node |
| 15-4 | Selecting HTML elements by class or tag name |
| 15-5 | Alphabetizing the elements of a list |
| 15-6 | Converting document content to uppercase |
| 15-7 | Reparenting a node to a <b> element |
| 15-8 | Using a DocumentFragment |
| 15-9 | A logging facility for client-side JavaScript |
| 15-10 | Element creation utility functions |
| 15-11 | Automatically generating a table of contents |
| 15-12 | Querying the currently selected text |
| Chapter 16: Cascading Style Sheets and Dynamic HTML | |
| 16-1 | Defining and using Cascading Style Sheets |
| 16-2 | Creating shadowed text with unobtrusive JavaScript |
| 16-3 | Displaying windows with CSS |
| 16-4 | Tooltips with CSS |
| 16-5 | A simple color-changing animation |
| 16-6 | A framework for CSS-based animations |
| 16-7 | Utility functions for manipulating className |
| 16-8 | Enabling and disabling stylesheets |
| 16-9 | Stylesheet utility methods |
| Chapter 17: Events and Event Handling | |
| 17-1 | One function, many event handlers |
| 17-2 | An event compatibility layer for IE |
| 17-3 | Tooltips positioned via mouse events |
| 17-4 | Dragging document elements |
| 17-5 | Restricting user input to a set of characters |
| 17-6 | A Keymap class for keyboard shortcuts |
| 17-7 | Portable event registration for onload event handlers |
| 17-8 | Dispatching synthetic events |
| Chapter 18: Forms and Form Elements | |
| 18-1 | An HTML form containing all form elements |
| 18-2 | Adding form validation to your HTML forms |
| 18-3 | Automatic form validation with unobtrusive JavaScript |
| Chapter 19: Cookies and Client-Side Persisence | |
| 19-1 | Using the Cookie class |
| 19-2 | A Cookie utility class |
| 19-3 | PObject.js: persistent objects for JavaScript |
| 19-4 | ActionScript code for Flash-based persistence |
| Chapter 20: Scripting HTTP | |
| 20-1 | The HTTP.newRequest() utility |
| 20-2 | The HTTP.getText() utility |
| 20-3 | The HTTP.getXML() utility |
| 20-4 | The HTTP.getHeaders() utility |
| 20-5 | The HTTP.post() utility |
| 20-6 | HTTP._getResponse() |
| 20-7 | The HTTP.get() utility |
| 20-8 | Ajax Tooltips |
| 20-9 | jsquoter.php |
| 20-10 | The HTTP.getTextWithScript() utility |
| http.js | The combined http.js module |
| Chapter 21: JavaScript and XML | |
| 21-1 | Creating an empty XML document |
| 21-2 | Loading an XML document synchronously |
| 21-3 | Loading an XML document asynchronously |
| 21-4 | Parsing an XML document |
| 21-5 | Getting an XML document from a data island |
| 21-6 | An XML data file |
| 21-7 | Building an HTML table from XML data |
| 21-8 | A simple XSL stylesheet |
| 21-9 | XSLT in Mozilla and Internet Explorer |
| 21-10 | Evaluating XPath Expressions |
| 21-11 | Serializing XML |
| 21-12 | An XML data island and HTML template |
| 21-13 | Expanding HTML templates |
| 21-14 | Querying a web service with SOAP |
| xml.js | The combined xml.js module |
| Chapter 22: Scripted Client-Side Graphics | |
| 22-1 | Adding a rollover effect to an image |
| 22-2 | Adding rollover effects unobtrusively |
| 22-3 | Initializing rollovers with XHTML and namespaces |
| 22-4 | Image animations |
| 22-5 | Drawing bar charts with CSS |
| 22-6 | The CSSDrawing class |
| 22-7 | Drawing a figure with the CSSDrawing class |
| 22-8 | Drawing a pie chart with JavaScript and SVG |
| 22-9 | SVG utility code |
| 22-10 | Drawing a pie chart with JavaScript and VML |
| 22-11 | Drawing a pie chart in an <canvas> tag |
| 22-12 | Canvas.as |
| 22-13 | Drawing a pie chart with JavaScript and Flash |
| 22-14 | A Java canvas applet for client-side drawing |
| 22-15 | Drawing a pie chart with JavaScript and Java |
| 22-16 | Creating a sparkline image with Javascript and Java |
| Chapter 23: Scripting Java Applets and Flash Movies | |
| 23-1 | An applet suitable for scripting |
| 23-2 | Scripting the Java Plug-in |
| 23-3 | An applet using the Common DOM API |
| 23-4 | ActionScript code that works with JavaScript |
| 23-5 | Scripting a Flash movie |
| 23-6 | ActionScript using ExternalInterface |
| 23-7 | Simplified Flash scripting with ExternalInterface |