JavaScript Cookbook

( 1 )


Problem solving with JavaScript is a lot trickier now that its use has expanded considerably in size, scope, and complexity. This cookbook has your back, with recipes for common tasks across the JavaScript world, whether you’re working in the browser, the server, or a mobile environment. Each recipe includes reusable code and practical advice for tackling JavaScript objects, Node, Ajax, JSON, data persistence, graphical and media applications, complex frameworks, modular ...

See more details below
$35.60 price
(Save 28%)$49.99 List Price

Pick Up In Store

Reserve and pick up in 60 minutes at your local store

Other sellers (Paperback)
  • All (11) from $23.95   
  • New (9) from $27.94   
  • Used (2) from $23.95   
JavaScript Cookbook

Available on NOOK devices and apps  
  • NOOK Devices
  • Samsung Galaxy Tab 4 NOOK 7.0
  • Samsung Galaxy Tab 4 NOOK 10.1
  • NOOK HD Tablet
  • NOOK HD+ Tablet
  • NOOK eReaders
  • NOOK Color
  • NOOK Tablet
  • Tablet/Phone
  • NOOK for Windows 8 Tablet
  • NOOK for iOS
  • NOOK for Android
  • NOOK Kids for iPad
  • PC/Mac
  • NOOK for Windows 8
  • NOOK for PC
  • NOOK for Mac
  • NOOK for Web

Want a NOOK? Explore Now

NOOK Book (eBook)
$24.99 price
(Save 41%)$42.99 List Price


Problem solving with JavaScript is a lot trickier now that its use has expanded considerably in size, scope, and complexity. This cookbook has your back, with recipes for common tasks across the JavaScript world, whether you’re working in the browser, the server, or a mobile environment. Each recipe includes reusable code and practical advice for tackling JavaScript objects, Node, Ajax, JSON, data persistence, graphical and media applications, complex frameworks, modular JavaScript, APIs, and many related technologies.

Aimed at people who have some experience with JavaScript, the first part covers traditional uses of JavaScript, along with new ideas and improved functionality. The second part dives into the server, mobile development, and a plethora of leading-edge tools. You’ll save time—and learn more about JavaScript in the process.

Topics include:

Classic JavaScript:

  • Arrays, functions, and the JavaScript Object
  • Accessing the user interface
  • Testing and accessibility
  • Creating and using JavaScript libraries
  • Client-server communication with Ajax
  • Rich, interactive web effects

JavaScript, All Blown Up:

  • New ECMAScript standard objects
  • Using Node on the server
  • Modularizing and managing JavaScript
  • Complex JavaScript frameworks
  • Advanced client-server communications
  • Visualizations and client-server graphics
  • Mobile application development
Read More Show Less

Product Details

  • ISBN-13: 9781491901885
  • Publisher: O'Reilly Media, Incorporated
  • Publication date: 2/13/2015
  • Edition number: 2
  • Pages: 634
  • Sales rank: 688,431
  • Product dimensions: 7.00 (w) x 9.10 (h) x 4.20 (d)

Meet the Author

Shelley Powers has been working with, and writing about, web technologies—from the first release of JavaScript to the latest graphics and design tools—for more than 12 years. Her recent O'Reilly books have covered the semantic web, Ajax, JavaScript, and web graphics. She's an avid amateur photographer and web development aficionado, who enjoys applying her latest experiments on her many web sites.

Read More Show Less

Table of Contents

Audience, Assumptions, and Approach;
Conventions Used in This Book;
Using Code Examples;
How to Contact Us;
Safari® Books Online;
Chapter 1: Working with JavaScript Strings;
1.1 Introduction;
1.2 Concatenating Two or More Strings;
1.3 Concatenating a String and Another Data Type;
1.4 Conditionally Comparing Strings;
1.5 Finding a Substring in a String;
1.6 Extracting a Substring from a String;
1.7 Checking for an Existing, Nonempty String;
1.8 Breaking a Keyword String into Separate Keywords;
1.9 Inserting Special Characters;
1.10 Processing Individual Lines of a textarea;
1.11 Trimming Whitespace from the Ends of a String;
1.12 Left- or Right-Pad a String;
Chapter 2: Using Regular Expressions;
2.1 Introduction;
2.2 Testing Whether a Substring Exists;
2.3 Testing for Case-Insensitive Substring Matches;
2.4 Validating a Social Security Number;
2.5 Finding and Highlighting All Instances of a Pattern;
2.6 Replacing Patterns with New Strings;
2.7 Swap Words in a String Using Capturing Parentheses;
2.8 Using Regular Expressions to Trim Whitespace;
2.9 Replace HTML Tags with Named Entities;
2.10 Searching for Special Characters;
Chapter 3: Dates, Time, and Timers;
3.1 Introduction;
3.2 Printing Out Today’s Date;
3.3 Printing Out the UTC Date and Time;
3.4 Printing Out an ISO 8601 Formatted Date;
3.5 Converting an ISO 8601 Formatted Date to a Format Acceptable to the Date Object;
3.6 Creating a Specific Date;
3.7 Scheduling a Future Date;
3.8 Tracking Elapsed Time;
3.9 Creating a Timeout;
3.10 Creating Recurring Timers;
3.11 Using Function Closures with Timers;
Chapter 4: Working with Numbers and Math;
4.1 Introduction;
4.2 Keeping an Incremental Counter;
4.3 Converting a Decimal to a Hexadecimal Value;
4.4 Creating a Random Number Generator;
4.5 Randomly Generating Colors;
4.6 Converting Strings in a Table to Numbers;
4.7 Summing All Numbers in a Table Column;
4.8 Converting Between Degrees and Radians;
4.9 Find the Radius and Center of a Circle to Fit Within a Page Element;
4.10 Calculating the Length of a Circular Arc;
Chapter 5: Working with Arrays and Loops;
5.1 Introduction;
5.2 Looping Through an Array;
5.3 Creating a Multidimensional Array;
5.4 Creating a String from an Array;
5.5 Sorting an Array;
5.6 Store and Access Values in Order;
5.7 Store and Access Values in Reverse Order;
5.8 Create a New Array as a Subset of an Existing Array;
5.9 Searching Through an Array;
5.10 Flatten a Multidimensional Array;
5.11 Search and Remove or Replace Array Elements;
5.12 Applying a Function Against Each Array Element;
5.13 Applying a Function to Every Element in an Array and Returning a New Array;
5.14 Creating a Filtered Array;
5.15 Validating Array Contents;
5.16 Using an Associative Array to Store Form Element Names and Values;
Chapter 6: Building Reusability with JavaScript Functions;
6.1 Introduction;
6.2 Creating a Block of Reusable Code;
6.3 Passing Single Data Values to and from a Function;
6.4 Passing Complex Data Objects to a Function;
6.5 Creating a Dynamic Runtime Function;
6.6 Passing a Function As an Argument to Another Function;
6.7 Implementing a Recursive Algorithm;
6.8 Create a Function That Remembers Its State;
6.9 Improving Application Performance with a Generalized Currying Function;
6.10 Improve Application Performance with Memoization (Caching Calculations);
6.11 Using an Anonymous Function to Wrap Global Variables;
Chapter 7: Handling Events;
7.1 Introduction;
7.2 Detecting When the Page Has Finished Loading;
7.3 Capturing the Location of a Mouse Click Event Using the Event Object;
7.4 Creating a Generic, Reusable Event Handler Function;
7.5 Canceling an Event Based on Changed Circumstance;
7.6 Preventing an Event from Propagating Through a Set of Nested Elements;
7.7 Capturing Keyboard Activity;
7.8 Using the New HTML5 Drag-and-Drop;
7.9 Using Safari Orientation Events and Other Mobile Development Environments;
Chapter 8: Browser Pieces;
8.1 Introduction;
8.2 Ask the Web Page Reader to Confirm an Action;
8.3 Creating a New, Stripped-Down Browser Window;
8.4 Finding Out About the Browser Accessing the Page;
8.5 Warning the Web Page Reader About Leaving a Page;
8.6 Changing Stylesheets Depending on Color Support;
8.7 Modifying Image Dimensions Depending on Page Size;
8.8 Creating Breadcrumbs in a CMS Template Page;
8.9 Bookmarking a Dynamic Page;
8.10 Preserving State for Back Button, Page Refresh;
Chapter 9: Form Elements and Validation;
9.1 Introduction;
9.2 Accessing Form Text Input Values;
9.3 Dynamically Disabling and Enabling Form Elements;
9.4 Getting Information from a Form Element Based on an Event;
9.5 Performing an Action When a Radio Button Is Clicked;
9.6 Checking for a Valid Phone Number;
9.7 Canceling a Form Submission;
9.8 Preventing Duplicate Form Submissions;
9.9 Hiding and Displaying Form Elements;
9.10 Modifying a Selection List Based on Other Form Decisions;
Chapter 10: Debugging and Error Handling;
10.1 Introduction;
10.2 Gracefully Handling No JavaScript Support;
10.3 Checking for Errors in Functions;
10.4 Using an Alert for Simple Debugging;
10.5 Catching an Error and Providing Graceful Error Handling;
10.6 Initiating Manageable Errors;
10.7 Using Firebug with Firefox;
10.8 Setting a Breakpoint and Examining Data with Firebug;
10.9 Firefox and the Console;
10.10 Using IE’s Built-in Debugger;
10.11 Setting a Breakpoint with IE Developer Tools;
10.12 Opera’s Dragonfly;
10.13 Setting a Breakpoint with Dragonfly;
10.14 Turning on Safari’s Development Tools;
10.15 Setting a Breakpoint with Safari’s Debugger;
10.16 Debugging in Chrome;
Chapter 11: Accessing Page Elements;
11.1 Introduction;
11.2 Access a Given Element and Find Its Parent and Child Elements;
11.3 Accessing All Images in the Web Page;
11.4 Discover All Images Within an Article;
11.5 Discover all Images in Articles Using the Selectors API;
11.6 Finding the Parent Element for a Group of Elements;
11.7 Highlighting the First Paragraph in Every Element;
11.8 Apply a Striping Theme to an Unordered List;
11.9 Creating an Array of All Elements of a Given Class;
11.10 Finding All Elements That Share an Attribute;
11.11 Finding All Checked Options;
11.12 Summing All the Values in a Table Row;
11.13 Get Element Attributes;
11.14 Get Style Information for an Element;
Chapter 12: Creating and Removing Elements and Attributes;
12.1 Introduction;
12.2 Using innerHTML: A Quick and Easy Approach to Adding Content;
12.3 Inserting Elements Before Existing Page Elements;
12.4 Appending a New Element to the End of a Page;
12.5 Triggering Older Versions of IE to Style New Elements;
12.6 Inserting a New Paragraph;
12.7 Adding Text to a New Paragraph;
12.8 Adding Attributes to an Existing Element;
12.9 Testing for a Boolean Attribute;
12.10 Removing an Attribute;
12.11 Moving a Paragraph;
12.12 Replacing Links with Footnote Bullets;
12.13 Adding Rows to an Existing Table;
12.14 Removing a Paragraph from a div Element;
12.15 Deleting Rows from an HTML Table;
12.16 Changing the Element’s CSS Style Properties;
Chapter 13: Working with Web Page Spaces;
13.1 Introduction;
13.2 Determining the Area of the Web Page;
13.3 Measuring Elements;
13.4 Locating Elements in the Page;
13.5 Hiding Page Sections;
13.6 Creating Collapsible Form Sections;
13.7 Adding a Page Overlay;
13.8 Creating Tab Pages;
13.9 Creating Hover-Based Pop-up Info Windows;
13.10 Collapsing or Resizing the Sidebar;
Chapter 14: Creating Interactive and Accessible Effects with JavaScript, CSS, and ARIA;
14.1 Introduction;
14.2 Displaying a Hidden Page Section;
14.3 Creating an Alert Message;
14.4 Highlighting Form Field with Missing or Incorrect Data;
14.5 Adding Keyboard Accessibility to a Page Overlay;
14.6 Creating Collapsible Form Sections;
14.7 Displaying a Flash of Color to Signal an Action;
14.8 Adding ARIA Attributes to a Tabbed Page Application;
14.9 Live Region;
Chapter 15: Creating Media Rich and Interactive Applications;
15.1 Introduction;
15.2 Creating Basic Shapes in Canvas (Using the canvas Element);
15.3 Implementing Canvas Applications in IE;
15.4 Creating a Dynamic Line Chart in Canvas;
15.5 Adding JavaScript to an SVG File;
15.6 Accessing SVG from Web Page Script;
15.7 Emulating SVG in Internet Explorer;
15.8 Enable Interactive SVG Embedded in HTML;
15.9 Using the Math Functions to Create a Realistic, Ticking Analog Clock in SVG;
15.10 Integrating SVG and the Canvas Element in HTML;
15.11 Turning on WebGL Support in Firefox and WebKit/Safari;
15.12 Running a Routine When an Audio File Begins Playing;
15.13 Controlling Video from JavaScript with the video Element;
Chapter 16: JavaScript Objects;
16.1 Introduction;
16.2 Defining a Basic JavaScript Object;
16.3 Keeping Object Members Private;
16.4 Expanding Objects with prototype;
16.5 Adding Getter/Setter to Objects;
16.6 Inheriting an Object’s Functionality;
16.7 Extending an Object by Defining a New Property;
16.8 Enumerating an Object’s Properties;
16.9 Preventing Object Extensibility;
16.10 Preventing Object Additions and Changes to Property Descriptors;
16.11 Preventing Any Changes to an Object;
16.12 One-Off Objects and Namespacing Your JavaScript;
16.13 Rediscovering “this” with Prototype.bind;
16.14 Chaining Your Object’s Methods;
Chapter 17: JavaScript Libraries;
17.1 Introduction;
17.2 Packaging Your Code;
17.3 Testing Your Code with JsUnit;
17.4 Minify Your Library;
17.5 Hosting Your Library;
17.6 Using an External Library: Building on the jQuery Framework;
17.7 Using Existing jQuery Plug-ins;
17.8 Convert Your Library to a jQuery Plug-in;
17.9 Safely Combining Several Libraries in Your Applications;
Chapter 18: Communication;
18.1 Introduction;
18.2 Accessing the XMLHttpRequest Object;
18.3 Preparing the Data for Transmission;
18.4 Determining the Type of Query Call;
18.5 Adding a Callback Function to an Ajax Request;
18.6 Checking for an Error Condition;
18.7 Processing a Text Result;
18.8 Making an Ajax Request to Another Domain (Using JSONP);
18.9 Populating a Selection List from the Server;
18.10 Using a Timer to Automatically Update the Page with Fresh Data;
18.11 Communicating Across Windows with PostMessage;
Chapter 19: Working with Structured Data;
19.1 Introduction;
19.2 Process an XML Document Returned from an Ajax Call;
19.3 Extracting Pertinent Information from an XML Tree;
19.4 Generate a JavaScript Object with JSON, Old-School Style;
19.5 Parse a JSON Formatted String;
19.6 Convert an Object to a Filtered/Transformed String with JSON;
19.7 Convert hCalendar Microformat Annotations into a Canvas Timeline;
19.8 Glean Page RDFa and Convert It into JSON Using rdfQuery and the jQuery RDF Plug-in;
Chapter 20: Persistence;
20.1 Introduction;
20.2 Attaching Persistent Information to URLs;
20.3 Creating a Cookie to Persist Information Across Pages;
20.4 Persisting Information Using the History.pushState Method and window.onpopevent;
20.5 Using sessionStorage for Client-Side Storage;
20.6 Creating a localStorage Client-Side Data Storage Item;
20.7 Persisting Data Using a Relational Data Store;
Chapter 21: JavaScript Outside the Box;
21.1 Introduction;
21.2 Creating a Browser Add-0n, Plug-in, or Extension;
21.3 Creating Desktop and Mobile Widgets;
21.4 Creating JavaScript Applications for the iPhone, Android, and BlackBerry with PhoneGap;
21.5 Enhancing Tools with JavaScript;
21.6 Creating Efficient Desktop Applications with Web Workers and the File API;

Read More Show Less

Customer Reviews

Average Rating 3
( 1 )
Rating Distribution

5 Star


4 Star


3 Star


2 Star


1 Star


Your Rating:

Your Name: Create a Pen Name or

Barnes & Review Rules

Our reader reviews allow you to share your comments on titles you liked, or didn't, with others. By submitting an online review, you are representing to Barnes & that all information contained in your review is original and accurate in all respects, and that the submission of such content by you and the posting of such content by Barnes & does not and will not violate the rights of any third party. Please follow the rules below to help ensure that your review can be posted.

Reviews by Our Customers Under the Age of 13

We highly value and respect everyone's opinion concerning the titles we offer. However, we cannot allow persons under the age of 13 to have accounts at or to post customer reviews. Please see our Terms of Use for more details.

What to exclude from your review:

Please do not write about reviews, commentary, or information posted on the product page. If you see any errors in the information on the product page, please send us an email.

Reviews should not contain any of the following:

  • - HTML tags, profanity, obscenities, vulgarities, or comments that defame anyone
  • - Time-sensitive information such as tour dates, signings, lectures, etc.
  • - Single-word reviews. Other people will read your review to discover why you liked or didn't like the title. Be descriptive.
  • - Comments focusing on the author or that may ruin the ending for others
  • - Phone numbers, addresses, URLs
  • - Pricing and availability information or alternative ordering information
  • - Advertisements or commercial solicitation


  • - By submitting a review, you grant to Barnes & and its sublicensees the royalty-free, perpetual, irrevocable right and license to use the review in accordance with the Barnes & Terms of Use.
  • - Barnes & reserves the right not to post any review -- particularly those that do not follow the terms and conditions of these Rules. Barnes & also reserves the right to remove any review at any time without notice.
  • - See Terms of Use for other conditions and disclaimers.
Search for Products You'd Like to Recommend

Recommend other products that relate to your review. Just search for them below and share!

Create a Pen Name

Your Pen Name is your unique identity on It will appear on the reviews you write and other website activities. Your Pen Name cannot be edited, changed or deleted once submitted.

Your Pen Name can be any combination of alphanumeric characters (plus - and _), and must be at least two characters long.

Continue Anonymously
Sort by: Showing 1 Customer Reviews
  • Posted September 21, 2010

    Well-constructed with forward looks to HTML5

    I have enjoyed other cookbooks in the O'Reilly library of computer texts, so when I saw JavaScript Cookbook by Shelley Powers in the catalog, I jumped at it. The description mentioned that the book would cover HTML5 and ECMAScript 5, so I really wanted to learn the new capabilities of both. I did get to learn a lot, but I wonder if these new features are too new. Like other cookbooks in the O'Reilly library, this one is organized as a series of specific problems, with their solutions neatly presented and grouped into the major chapters. Each solution has a discussion to flesh out the details. The website has downloadable copies of the examples in the book, which I used to test out the various recipes. ECMAScript 5 is fairly new, and HTML5 is still under development, so I made sure I had the latest stable versions of the major browsers (Firefox, Google Chrome, Internet Explorer, Opera, and Safari) to see how they would cope. The HTML5 features are very sparsely supported as of yet, so those portions of the book should be considered more of a sampling of things to come rather than a definitive set of solutions. They start out easily enough with recipes for handling JavaScript strings. However, the discussion of String objects and literals obviously implies that the reader is already somewhat familiar with Object terminology and functionality. That makes this book unsuitable for beginners. While much of the book is applicable to today's browsers, there is a lot of coverage of the new capabilities made available in the new HTML5 specifications. Unfortunately, most browsers either do not support, or only partially support these features, so the information is only useful as a "taste of things to come". As I step back and reflect on this book, I think that many topics are solutions that cannot be implemented because the typically available browsers don't support the new features yet. If a web page designer wants their site to be available to users now, they need to focus on the features that are well entrenched across the internet. Having so many solutions based on features and capabilities that are still being defined is only useful as academic exercises. I would have preferred that the JavaScript Cookbook be more useful for the state of the web right now. That is why I'm rating it only 3 out of 5.

    Was this review helpful? Yes  No   Report this review
Sort by: Showing 1 Customer Reviews

If you find inappropriate content, please report it to Barnes & Noble
Why is this product inappropriate?
Comments (optional)