Log In
Or create an account ->
Imperial Library
Home
About
News
Upload
Forum
Help
Login/SignUp
Index
JavaScript Cookbook
The World of JavaScript
Book Audience
Book Architecture or Why Is This Book Organized in This Way?
Conventions Used in This Book
Using Code Examples
Safari® Books Online
How to Contact Us
Acknowledgments
I. Classic JavaScript
1. The JavaScript Not-So-Simple Building Blocks
Differentiating Between a JavaScript Object, Primitive, and Literal
Extracting a List from a String
Checking for an Existing, Nonempty String
Inserting Special Characters
Replacing Patterns with New Strings
Finding and Highlighting All Instances of a Pattern
Swapping Words in a String Using Capturing Parentheses
Replacing HTML Tags with Named Entities
Converting an ISO 8601 Formatted Date to a Date Object Acceptable Format
Using Function Closures with Timers
Tracking Elapsed Time
Converting a Decimal to a Hexadecimal Value
Summing All Numbers in a Table Column
Converting Between Degrees and Radians
Find the Radius and Center of a Circle to Fit Within a Page Element
Calculating the Length of a Circular Arc
Using ES6 String Extras Without Leaving Users in the Dirt
2. JavaScript Arrays
Searching Through an Array
Flattening a Two-Dimensional Array with concat() and apply()
Removing or Replacing Array Elements
Extracting a Portion of an Array
Applying a Function Against Each Array Element
Traversing the Results from querySelectorAll() with forEach() and call()
Applying a Function to Every Element in an Array and Returning a New Array
Creating a Filtered Array
Validating Array Contents
Using an Associative Array to Store Form Element Names and Values
Using a Destructuring Assignment to Simplify Code
3. Functions: The JavaScript Building Blocks
Placing Your Function and Hoisting
Passing a Function As an Argument to Another Function
Implementing a Recursive Algorithm
Preventing Code Blocking with a Timer and a Callback
Creating a Function That Remembers Its State
Converting Function Arguments into an Array
Reducing Redundancy by Using a Partial Application
Improving Application Performance with Memoization (Caching Calculations)
Using an Anonymous Function to Wrap Global Variables
Providing a Default Parameter
4. The Malleable JavaScript Object
Keeping Object Members Private
Using Prototype to Create Objects
Inheriting an Object’s Functionality
Extending an Object by Defining a New Property
Preventing Object Extensibility
Preventing Any Changes to an Object
Namespacing Your JavaScript Objects
Rediscovering this with Prototype.bind
Chaining Your Object’s Methods
5. JavaScript and Directly Accessing the User Interface
Accessing a Given Element and Finding Its Parent and Child Elements
Accessing All Images in the Web Page
Discovering All Images in Articles Using the Selectors API
Setting an Element’s Style Attribute
Applying a Striped Theme to an Unordered List
Finding All Elements That Share an Attribute
Inserting a New Paragraph
Adding Text to a New Paragraph
Deleting Rows from an HTML Table
Adding a Page Overlay
Creating Collapsible Form Sections
Hiding Page Sections
Creating Hover-Based Pop-Up Info Windows
Displaying a Flash of Color to Signal an Action
6. Preliminary Testing and Accessibility
Cleaning Up Your Code with JSHint
Unit Testing Your Code with QUnit
Testing Your Application in Various Environments
Performance Testing Different Coding Techniques
Highlighting Errors Accessibly
Creating an Accessible Automatically Updated Region
7. Creating and Using JavaScript Libraries
Finding the Perfect Library
Testing for Features with Modernizr.load
Going Beyond the Math Object’s Capability
Finding the Number of Days Between Two Dates
Using an External Library: Building on the jQuery Framework
Using a jQuery Plugin
Handling Keyboard Shortcuts with Mousetrap
Utilizing the Utility Library Underscore
Packaging Your Code
Adding Support for Private Data Members
Minify Your Library
Hosting Your Library
Serving Code from a CDN
Convert Your Library to a jQuery Plug-in
Safely Combining Several Libraries in Your Applications
8. Simplified Client-Server Communication and Data
Handling an XML Document Returned via an Ajax Call
Extracting Pertinent Information from an XML Tree
Parsing and Automatically Modifying JSON
Converting an Object to a Filtered/Transformed String with JSON
Making an Ajax Request to Another Domain (Using JSONP)
Processing JSON from an Ajax Request
Populating a Selection List from the Server
Using a Timer to Automatically Update the Page with Fresh Data
9. Creating Media Rich, Interactive Web Effects
Creating a Dynamic Line Chart in Canvas
Adding JavaScript to SVG
Accessing SVG from Web Page Script
Integrating SVG and the Canvas Element in HTML
Running a Routine When an Audio File Begins Playing
Controlling Video from JavaScript with the video Element
Adding Filter Effects to Video via Canvas
II. JavaScript, All Blown Up
10. The New ECMAScript Standard Objects
Using let in Your Browser Application
Creating a Collection of Non-Duplicated Values
Creating Unique Key/Value Pairs with Uniquely Different Keys
Creating Absolutely Unique Object Property Keys
Iterating Through Tasks Made Easy
Creating Functions that Gracefully Yield
Implementing Just-in-Time Object Behavioral Modifications with Proxies
Creating a True Class and Extending It (with a Little Help from Traceur)
Using Promises for Efficient Asynchronous Processing
11. Node: JavaScript on the Server
Responding to a Simple Browser Request
Serving Up Formatted Data
Reading and Writing File Data
Using let and Other ES 6 Additions in Node
Interactively Trying Out Node Code Snippets with REPL
Getting Input from the Terminal
Working with Node Timers and Understanding the Node Event Loop
Managing Callback Hell
Accessing Command-Line Functionality Within a Node Application
Running Node and Apache on the Same Port
Keeping a Node Instance Up and Running
Monitoring Application Changes and Restarting
Screen Scraping with Request
Creating a Command-Line Utility with Help From Commander
12. Modularizing and Managing JavaScript
Loading Scripts with a Script Loader
Loading Scripts Asynchronously the HTML5 Way
Converting Your JavaScript to AMD and RequireJS
Using RequireJS with jQuery or Another Library
Loading and Using Dojo Modules
Installing and Maintaining Node Modules with npm
Searching for a Specific Node Module via npm
Converting Your Library into a Node Module
Taking Your Code Across All Module Environments
Creating an Installable Node Module
Packaging and Managing Your Client-Side Dependencies with Bower
Compiling Node.js Modules for Use in the Browser with Browserify
Unit Testing Your Node Modules
Running Tasks with Grunt
13. Fun with APIs
Accessing JSON-Formatted Data via a RESTful API
Creating a RESTFul API with Restify
Enabling a Mobile-Like Notification in the Desktop Browser
Loading a File Locally in the Browser
Creating a Mini E-Pub Reader Using Web Workers and the File API
Exploring Google Maps and Other APIS
Accessing Twitter’s API from a Node Application
14. JavaScript Frameworks
Using the Express-Generator to Generate an Express Site
Converting a Generated Express Site into a Basic MVC App
Choosing a SPA Framework: Deconstructing the TodoMVC
Working with the OAuth Framework
Extending the Possible with Web Components
15. Advanced Client-Server Communications and Streams
Allowing Cross-Domain Requests
Implementing a PUT Request in Ajax
Sending Binary Data Through Ajax and Loading into an Image
Sharing HTTP Cookies Across Domains
Establishing Two-Way Communication Between Client and Server
Unloading and Zipping Files Using Transform Streams
Testing the Performance and Capability of Your WebSockets Application
16. Data Visualizations and Client/Server Graphics
Creating an SVG Bar Chart with D3
Mapping Data Point Variations with a Radar Chart
Feeding a Scrolling Timeline via WebSocket
Generating Screenshots of Generated Web Page Content (PhantomJS)
Converting Graphics to Text (Ocrad.js)
Cropping (or Otherwise Modifying) Uploaded Images
17. Data and Persistence
Validating Form Data
Persisting Information Using HTML5
Using sessionStorage for Client-Side Storage
Creating a localStorage Client-Side Data Storage Item
Using Squel.js to Query a MySQL Database
Persisting Larger Chunks of Data on the Client Using IndexedDB
Accessing Data in the Cloud Using Dropbox Datastores
18. JavaScript Hits the (Mobile) Road
Creating an Installable, Hosted Web App
Packaging Web Apps for the Amazon Appstore
Building a Basic Android App Using Cordova (PhoneGap)
Porting Where Am I? to Android
Creating a Geolocation Firefox OS App
Porting the Geolocation App to a Google Chrome App
Publishing Your Geolocation App in the Kindle Fire OS Environment
Debugging Your Android or Amazon Fire OS App
Getting Information About the Device
A. Up and Running in jsBin and jsFiddle
The Console Is Your Friend
jsBin and jsFiddle: Online JavaScript Playgrounds
Index
Colophon
Copyright
← Prev
Back
Next →
← Prev
Back
Next →