jQuery Cookbook

This book review was published by Slashdot, .

jQuery Cookbook

Like all major programming languages, JavaScript can be extended in functionality through the use of libraries, such as jQuery, which is currently seeing tremendous popularity and enhancement in the Web development community. Designers and developers who want to learn how to use jQuery for creating rich user interfaces through client-side JavaScript, are advised to begin their journey to jQuery proficiency by reading one of the many books dedicated to this powerful JavaScript resource — such as jQuery Cookbook: Solutions & Examples for jQuery Developers.

This book was published by O'Reilly Media on 19 November 2009, under the ISBN 978-0596159771, and is authored by no fewer than 19 contributors — all quite knowledgeable of JavaScript and jQuery — and edited by one of those contributors, Cody Lindley. On the publisher's page for the book, visitors can read the book's description, table of contents, and errata, of which there are 22 as of this writing, although none have been confirmed by the authors or publisher. Visitors can also download all of the sample code used in the book, in addition to the eight code demos for Chapter 13. Lastly, prospective buyers can purchase the print version of the book, the electronic one (in PDF, ePub, and Mobi format), or the two combined at a considerable discount — or read the book as part of Safari Books Online.

jQuery Cookbook's 480 pages are organized into 18 chapters, covering a wide range of topics: the basics, element selection, utilities, dimensions, effects, events, forms, plugins, user interfaces, theming, Ajax, data formats, and testing. Lindley starts off the first chapter, titled "jQuery Basics", by presenting the advantages, philosophy, and organization of jQuery. Readers will likely chuckle at his suggestion that they memorize the jQuery API outline, which comprises two pages. The many code snippets are quite helpful, but they are needlessly long, partly because most of them contain far more HTML elements than are needed to illustrate the point, and also because each snippet contains the DOCTYPE and head tags, among others — often taking up more lines on the page than does the code pertinent to the topic at hand. This pointless and space-wasting redundancy is seen also in a few of the chapters that follow. As well, some of the passages in the first chapter's narrative are oddly phrased, frequently requiring a rereading of the material, while others could be made more concise. Additionally, some of the sample code contains bugs, which fortunately are detailed on the errata page mentioned earlier. The second chapter, "Selecting Elements with jQuery", presents numerous techniques for specifying elements within the DOM. The only obvious blemish in the material is in section 2.4, where the author refers to animated elements, but with absolutely no explanation as to what that means; countless new readers may assume he is referring to animated GIFs. Nonetheless, these two chapters form a helpful foundation for the rest of the manuscript.

The third chapter, "Beyond the Basics", gets off to a questionable start with the assertion that "jQuery can […] extend jQuery to infinite possibilities", which sounds like a line wisely rejected for the movie Toy Story. Yet the discussion continues on a solid footing, as it covers more advanced techniques for working with selected elements. Some of the discussion overlaps material presented in the previous two chapters, but it is always worthwhile to hear critical concepts explained from a different perspective. However, section 3.8, which briefly introduces jQuery plugins, is out of place; that material should be folded into Chapter 12, which focuses on that topic. The fourth chapter may be brief, but it explains several jQuery utility methods. Most of the code snippets use a format of "(function($) … (jQuery);" — whose usage and advantages are not explained in this chapter, nor any earlier ones. This points up one of the key downsides of having almost every chapter of a programming book written by separate authors: readers can be confused or misled by disparities in coding practices, especially when the reasoning behind them is not given. The title of the fifth chapter, "Faster, Simpler, More Fun", is a bit misleading, because the authors don't explain how to make one's jQuery programming simpler or more fun, but they do provide a great deal of information on troubleshooting, performance optimization, and jQuery coding practices, including those pertaining to progressive enhancement, accessibility, and unobtrusiveness. Section 5.19 lacks a figure showing the menu being discussed, but that's the only obvious flaw.

The remaining chapters are dedicated to more specific aspects of jQuery programming, including the important topics of page layout as well as element positioning and sizing, discussed in Chapter 6. The subsequent chapter delves into effects, which is one of the more exciting topics in the jQuery realm. Even though a portion of the readers may be put off by the trickiness of the code, the material does demonstrate some of the powerful capabilities of jQuery effects — which in conjunction with HTML5 may easily encroach on areas of client-side programming formerly dominated by Adobe Flash. Throughout Chapter 7, most if not all places where the author refers to the "mouse", he apparently means the "mouse pointer". Events play a critical role in JavaScript software, and even more so for code that is mostly jQuery — thus the value of chapters 8 and 9, which examine basic and advanced event handling. The next pair of chapters discuss a variety of techniques for enhancing HTML forms, from scratch and through the use of jQuery plugins. The sample source code is better commented than what is seen elsewhere in the book, and the explanations are quite good. The subsequent chapter focuses on jQuery plugins, beyond their usage within HTML forms, and briefly explains how to create your own plugins and how to perform unit testing on them.

Because JavaScript is primarily a client-side technology, it should come as no surprise that jQuery can prove an outstanding tool in crafting user interfaces for websites and web-based applications. Chapters 13 through 15 explore such topics as drop-down menus, sliding panels, rotating images, modal windows, tooltips, the jQuery UI, and how to style jQuery UI widgets, a.k.a., theming. No Web interface is an island, and over the years there have emerged a number of data formats and protocols for utilizing those formats for transmitting information between browsers and servers — such as Ajax, XML, JSON, and JSONP — covered in Chapter 16. Finally, the last two chapters of the book are geared more to testing and deployment, and less so to interface design and development. The topics covered include techniques for persisting data in the browser, managing large amounts of code and data for major software projects, automating the unit testing of jQuery code, testing callbacks and user actions, grouping and selecting tests, and more.

Overall, jQuery Cookbook starts off with some basics, and only then moves on to higher-level concepts and related use cases. However, the book is ostensibly aimed at beginners and intermediate JavaScript programmers, but the former group may find the ideas difficult to grasp fully, despite the introductory chapters — because some of the techniques are fairly advanced, they involve terminology unfamiliar to anyone new to jQuery, and some of that terminology is not explained. On the other hand, the recipes are generally well written and clear, supplemented with properly tested and working code. Consequently, anyone who takes the time to work through the examples patiently, should be well rewarded.

Because of its coverage of a wide range of topics, jQuery Cookbook can be used not only as a learning aid, but in some respects also as a reference — and in this regard the book's index will be quite useful. In light of the considerable length of the manuscript, reading it from stem to stern would involve an investment of time — especially if one were to work through all of the examples and try them out in one's own development environment — quite easily, in fact, since all of it can be downloaded from the publisher's site. Most of it, however, is organized as plain text files, and not HTML files; and no reason is provided for this annoying choice.

In terms of the layout and appearance of the text and figures, one flaw is that in countless lines throughout the book, the words are jammed together, making it difficult to read the text rapidly. In fact, some of these lines almost look like single words. This is also seen in the subheads, an excellent example of which can be found on page 149: "Solution2:ChangingthehrefAttributeoftheStylesheetThat'sResponsible". Unfortunately, the same is true for much of the source code, but in a different sense: Operators and variables are jammed together, clearly illustrating the need for whitespace in making code more readable. Some of the code is excessively long (noted earlier), and the authors are inconsistent as to whether their JavaScript is placed at the end of the body element or in the header element. Nonetheless, the sample code is generally of good quality.

There is another aspect related to not only this book but all other computer programming books for which individual chapters are written by different authors: jQuery Cookbook does not seem to be a single book, but instead a collection of books that were bundled together because of a common thread, namely, jQuery. This leads to some of the problems mentioned earlier, such as discrepancies in coding techniques and formatting — from which the beginning reader is supposedly learning best practices. On the other hand, the multi-author approach makes it possible for each major subject area to be handled by one or more writers who are expert in that particular area — which in turn results in a better product overall, even if one or two of the chapters are noticeably weaker than the others.

The book contains a number of copyediting flaws not listed on the aforementioned errata page: "elevated" should instead read "alleviated" (page 12); "or [its] alias" (13); "could change" should read "could be changed" (26); "jQuery('a')removeAttr('title')" is missing a "." (30); "'blue')" is missing a terminating ";" (50); "season in" should read "season" (56); "was contained" should read "is contained" (144); "position: absolute" in the narrative should not be broken between two lines (156); "great[er] than" (157); "equal[-]sized panels" (160); "only running" should read "only runs" (165); "still support[s]" (168), "#source5txt" should read "#source4txt" (217); and at this point I stopped recording errata. Also, in countless places in most of the chapters, semicolons are used where dashes are called for, and vice versa. O'Reilly's copyeditors should have detected and fixed those errors prior to publication.

Yet most of these blemishes are of little significance. What really counts is the overall value provided to the reader: Usable for both learning and reference, jQuery Cookbook delivers a tasty buffet of programming essentials, best practices, illustrative examples, optimization tips, and other information of value to JavaScript developers who wish to spice up their Web creations with jQuery.

Copyright © 2010 Michael J. Ross. All rights reserved.
bad bots block