Book description
Why reinvent the wheel every time you run into a problem with JavaScript? This cookbook is chock-full of code recipes that address common programming tasks, as well as techniques for building web apps that work in any browser. Just copy and paste the code samples into your project—you’ll get the job done faster and learn more about JavaScript in the process.
You'll also learn how to take advantage of the latest features in ECMAScript 5 and HTML5, including the new cross-domain widget communication technique, HTML5's video and audio elements, and the drawing canvas. You'll find recipes for using these features with JavaScript to build high-quality application interfaces.
- Create interactive web and desktop applications
- Work with JavaScript objects, such as String, Array, Number, and Math
- Use JavaScript with Scalable Vector Graphics (SVG) and the canvas element
- Store data in various ways, from the simple to the complex
- Program the new HTML5 audio and video elements
- Implement concurrent programming with Web Workers
- Use and create jQuery plug-ins
- Use ARIA and JavaScript to create fully accessible rich internet applications
Publisher resources
Table of contents
- A Note Regarding Supplemental Files
- Preface
-
1. Working with JavaScript Strings
- 1.0. Introduction
- 1.1. Concatenating Two or More Strings
- 1.2. Concatenating a String and Another Data Type
- 1.3. Conditionally Comparing Strings
- 1.4. Finding a Substring in a String
- 1.5. Extracting a Substring from a String
- 1.6. Checking for an Existing, Nonempty String
- 1.7. Breaking a Keyword String into Separate Keywords
- 1.8. Inserting Special Characters
- 1.9. Processing Individual Lines of a textarea
- 1.10. Trimming Whitespace from the Ends of a String
- 1.11. Left- or Right-Pad a String
-
2. Using Regular Expressions
- 2.0. Introduction
- 2.1. Testing Whether a Substring Exists
- 2.2. Testing for Case-Insensitive Substring Matches
- 2.3. Validating a Social Security Number
- 2.4. Finding and Highlighting All Instances of a Pattern
- 2.5. Replacing Patterns with New Strings
- 2.6. Swap Words in a String Using Capturing Parentheses
- 2.7. Using Regular Expressions to Trim Whitespace
- 2.8. Replace HTML Tags with Named Entities
- 2.9. Searching for Special Characters
-
3. Dates, Time, and Timers
- 3.0. Introduction
- 3.1. Printing Out Today’s Date
- 3.2. Printing Out the UTC Date and Time
- 3.3. Printing Out an ISO 8601 Formatted Date
- 3.4. Converting an ISO 8601 Formatted Date to a Format Acceptable to the Date Object
- 3.5. Creating a Specific Date
- 3.6. Scheduling a Future Date
- 3.7. Tracking Elapsed Time
- 3.8. Creating a Timeout
- 3.9. Creating Recurring Timers
- 3.10. Using Function Closures with Timers
-
4. Working with Numbers and Math
- 4.0. Introduction
- 4.1. Keeping an Incremental Counter
- 4.2. Converting a Decimal to a Hexadecimal Value
- 4.3. Creating a Random Number Generator
- 4.4. Randomly Generating Colors
- 4.5. Converting Strings in a Table to Numbers
- 4.6. Summing All Numbers in a Table Column
- 4.7. Converting Between Degrees and Radians
- 4.8. Find the Radius and Center of a Circle to Fit Within a Page Element
- 4.9. Calculating the Length of a Circular Arc
-
5. Working with Arrays and Loops
- 5.0. Introduction
- 5.1. Looping Through an Array
- 5.2. Creating a Multidimensional Array
- 5.3. Creating a String from an Array
- 5.4. Sorting an Array
- 5.5. Store and Access Values in Order
- 5.6. Store and Access Values in Reverse Order
- 5.7. Create a New Array as a Subset of an Existing Array
- 5.8. Searching Through an Array
- 5.9. Flatten a Multidimensional Array
- 5.10. Search and Remove or Replace Array Elements
- 5.11. Applying a Function Against Each Array Element
- 5.12. Applying a Function to Every Element in an Array and Returning a New Array
- 5.13. Creating a Filtered Array
- 5.14. Validating Array Contents
- 5.15. Using an Associative Array to Store Form Element Names and Values
-
6. Building Reusability with JavaScript Functions
- 6.0. Introduction
- 6.1. Creating a Block of Reusable Code
- 6.2. Passing Single Data Values to and from a Function
- 6.3. Passing Complex Data Objects to a Function
- 6.4. Creating a Dynamic Runtime Function
- 6.5. Passing a Function As an Argument to Another Function
- 6.6. Implementing a Recursive Algorithm
- 6.7. Create a Function That Remembers Its State
- 6.8. Improving Application Performance with a Generalized Currying Function
- 6.9. Improve Application Performance with Memoization (Caching Calculations)
- 6.10. Using an Anonymous Function to Wrap Global Variables
-
7. Handling Events
- 7.0. Introduction
- 7.1. Detecting When the Page Has Finished Loading
- 7.2. Capturing the Location of a Mouse Click Event Using the Event Object
- 7.3. Creating a Generic, Reusable Event Handler Function
- 7.4. Canceling an Event Based on Changed Circumstance
- 7.5. Preventing an Event from Propagating Through a Set of Nested Elements
- 7.6. Capturing Keyboard Activity
- 7.7. Using the New HTML5 Drag-and-Drop
- 7.8. Using Safari Orientation Events and Other Mobile Development Environments
-
8. Browser Pieces
- 8.0. Introduction
- 8.1. Ask the Web Page Reader to Confirm an Action
- 8.2. Creating a New, Stripped-Down Browser Window
- 8.3. Finding Out About the Browser Accessing the Page
- 8.4. Warning the Web Page Reader About Leaving a Page
- 8.5. Changing Stylesheets Depending on Color Support
- 8.6. Modifying Image Dimensions Depending on Page Size
- 8.7. Creating Breadcrumbs in a CMS Template Page
- 8.8. Bookmarking a Dynamic Page
- 8.9. Preserving State for Back Button, Page Refresh
-
9. Form Elements and Validation
- 9.0. Introduction
- 9.1. Accessing Form Text Input Values
- 9.2. Dynamically Disabling and Enabling Form Elements
- 9.3. Getting Information from a Form Element Based on an Event
- 9.4. Performing an Action When a Radio Button Is Clicked
- 9.5. Checking for a Valid Phone Number
- 9.6. Canceling a Form Submission
- 9.7. Preventing Duplicate Form Submissions
- 9.8. Hiding and Displaying Form Elements
- 9.9. Modifying a Selection List Based on Other Form Decisions
-
10. Debugging and Error Handling
- 10.0. Introduction
- 10.1. Gracefully Handling No JavaScript Support
- 10.2. Checking for Errors in Functions
- 10.3. Using an Alert for Simple Debugging
- 10.4. Catching an Error and Providing Graceful Error Handling
- 10.5. Initiating Manageable Errors
- 10.6. Using Firebug with Firefox
- 10.7. Setting a Breakpoint and Examining Data with Firebug
- 10.8. Firefox and the Console
- 10.9. Using IE’s Built-in Debugger
- 10.10. Setting a Breakpoint with IE Developer Tools
- 10.11. Opera’s Dragonfly
- 10.12. Setting a Breakpoint with Dragonfly
- 10.13. Turning on Safari’s Development Tools
- 10.14. Setting a Breakpoint with Safari’s Debugger
- 10.15. Debugging in Chrome
-
11. Accessing Page Elements
- 11.0. Introduction
- 11.1. Access a Given Element and Find Its Parent and Child Elements
- 11.2. Accessing All Images in the Web Page
- 11.3. Discover All Images Within an Article
- 11.4. Discover all Images in Articles Using the Selectors API
- 11.5. Finding the Parent Element for a Group of Elements
- 11.6. Highlighting the First Paragraph in Every Element
- 11.7. Apply a Striping Theme to an Unordered List
- 11.8. Creating an Array of All Elements of a Given Class
- 11.9. Finding All Elements That Share an Attribute
- 11.10. Finding All Checked Options
- 11.11. Summing All the Values in a Table Row
- 11.12. Get Element Attributes
- 11.13. Get Style Information for an Element
-
12. Creating and Removing Elements and Attributes
- 12.0. Introduction
- 12.1. Using innerHTML: A Quick and Easy Approach to Adding Content
- 12.2. Inserting Elements Before Existing Page Elements
- 12.3. Appending a New Element to the End of a Page
- 12.4. Triggering Older Versions of IE to Style New Elements
- 12.5. Inserting a New Paragraph
- 12.6. Adding Text to a New Paragraph
- 12.7. Adding Attributes to an Existing Element
- 12.8. Testing for a Boolean Attribute
- 12.9. Removing an Attribute
- 12.10. Moving a Paragraph
- 12.11. Replacing Links with Footnote Bullets
- 12.12. Adding Rows to an Existing Table
- 12.13. Removing a Paragraph from a div Element
- 12.14. Deleting Rows from an HTML Table
- 12.15. Changing the Element’s CSS Style Properties
-
13. Working with Web Page Spaces
- 13.0. Introduction
- 13.1. Determining the Area of the Web Page
- 13.2. Measuring Elements
- 13.3. Locating Elements in the Page
- 13.4. Hiding Page Sections
- 13.5. Creating Collapsible Form Sections
- 13.6. Adding a Page Overlay
- 13.7. Creating Tab Pages
- 13.8. Creating Hover-Based Pop-up Info Windows
- 13.9. Collapsing or Resizing the Sidebar
-
14. Creating Interactive and Accessible Effects with JavaScript, CSS, and
ARIA
- 14.0. Introduction
- 14.1. Displaying a Hidden Page Section
- 14.2. Creating an Alert Message
- 14.3. Highlighting Form Field with Missing or Incorrect Data
- 14.4. Adding Keyboard Accessibility to a Page Overlay
- 14.5. Creating Collapsible Form Sections
- 14.6. Displaying a Flash of Color to Signal an Action
- 14.7. Adding ARIA Attributes to a Tabbed Page Application
- 14.8. Live Region
-
15. Creating Media Rich and Interactive Applications
- 15.0. Introduction
- 15.1. Creating Basic Shapes in Canvas (Using the canvas Element)
- 15.2. Implementing Canvas Applications in IE
- 15.3. Creating a Dynamic Line Chart in Canvas
- 15.4. Adding JavaScript to an SVG File
- 15.5. Accessing SVG from Web Page Script
- 15.6. Emulating SVG in Internet Explorer
- 15.7. Enable Interactive SVG Embedded in HTML
- 15.8. Using the Math Functions to Create a Realistic, Ticking Analog Clock in SVG
- 15.9. Integrating SVG and the Canvas Element in HTML
- 15.10. Turning on WebGL Support in Firefox and WebKit/Safari
- 15.11. Running a Routine When an Audio File Begins Playing
- 15.12. Controlling Video from JavaScript with the video Element
-
16. JavaScript Objects
- 16.0. Introduction
- 16.1. Defining a Basic JavaScript Object
- 16.2. Keeping Object Members Private
- 16.3. Expanding Objects with prototype
- 16.4. Adding Getter/Setter to Objects
- 16.5. Inheriting an Object’s Functionality
- 16.6. Extending an Object by Defining a New Property
- 16.7. Enumerating an Object’s Properties
- 16.8. Preventing Object Extensibility
- 16.9. Preventing Object Additions and Changes to Property Descriptors
- 16.10. Preventing Any Changes to an Object
- 16.11. One-Off Objects and Namespacing Your JavaScript
- 16.12. Rediscovering “this” with Prototype.bind
- 16.13. Chaining Your Object’s Methods
-
17. JavaScript Libraries
- 17.0. Introduction
- 17.1. Packaging Your Code
- 17.2. Testing Your Code with JsUnit
- 17.3. Minify Your Library
- 17.4. Hosting Your Library
- 17.5. Using an External Library: Building on the jQuery Framework
- 17.6. Using Existing jQuery Plug-ins
- 17.7. Convert Your Library to a jQuery Plug-in
- 17.8. Safely Combining Several Libraries in Your Applications
-
18. Communication
- 18.0. Introduction
- 18.1. Accessing the XMLHttpRequest Object
- 18.2. Preparing the Data for Transmission
- 18.3. Determining the Type of Query Call
- 18.4. Adding a Callback Function to an Ajax Request
- 18.5. Checking for an Error Condition
- 18.6. Processing a Text Result
- 18.7. Making an Ajax Request to Another Domain (Using JSONP)
- 18.8. Populating a Selection List from the Server
- 18.9. Using a Timer to Automatically Update the Page with Fresh Data
- 18.10. Communicating Across Windows with PostMessage
-
19. Working with Structured Data
- 19.0. Introduction
- 19.1. Process an XML Document Returned from an Ajax Call
- 19.2. Extracting Pertinent Information from an XML Tree
- 19.3. Generate a JavaScript Object with JSON, Old-School Style
- 19.4. Parse a JSON Formatted String
- 19.5. Convert an Object to a Filtered/Transformed String with JSON
- 19.6. Convert hCalendar Microformat Annotations into a Canvas Timeline
- 19.7. Glean Page RDFa and Convert It into JSON Using rdfQuery and the jQuery RDF Plug-in
-
20. Persistence
- 20.0. Introduction
- 20.1. Attaching Persistent Information to URLs
- 20.2. Creating a Cookie to Persist Information Across Pages
- 20.3. Persisting Information Using the History.pushState Method and window.onpopevent
- 20.4. Using sessionStorage for Client-Side Storage
- 20.5. Creating a localStorage Client-Side Data Storage Item
- 20.6. Persisting Data Using a Relational Data Store
-
21. JavaScript Outside the Box
- 21.0. Introduction
- 21.1. Creating a Browser Add-0n, Plug-in, or Extension
- 21.2. Creating Desktop and Mobile Widgets
- 21.3. Creating JavaScript Applications for the iPhone, Android, and BlackBerry with PhoneGap
- 21.4. Enhancing Tools with JavaScript
- 21.5. Creating Efficient Desktop Applications with Web Workers and the File API
- Index
- About the Author
- Colophon
- Copyright
Product information
- Title: JavaScript Cookbook
- Author(s):
- Release date: July 2010
- Publisher(s): O'Reilly Media, Inc.
- ISBN: 9780596806132
You might also like
book
JavaScript: The Good Parts
Most programming languages contain good and bad parts, but JavaScript has more than its share of …
book
JavaScript Cookbook, 3rd Edition
Why reinvent the wheel every time you run into a problem with JavaScript? This cookbook is …
book
Learning JavaScript Design Patterns
With Learning JavaScript Design Patterns, you’ll learn how to write beautiful, structured, and maintainable JavaScript by …
book
The Joy of JavaScript
Whether building interactive browser-based applications or creating server-side applications in Node, JavaScript is the most widely …