John McCollum

jQuery 1.4 Reference Guide – reviewed

Packt Publishing were kind enough to send me a copy of the jQuery 1.4 Reference Guide, by Jonathan Chaffer and Karl Swedberg. The book is intended to be a comprehensive reference for the library, aimed at intermediate to advanced readers.

The first point I should make about this book is that it is not intended for users who are new to the jQuery library – this is not a book full of tutorials. If that’s what you’re looking for, you should check out Learning jQuery 1.3 instead, by the same authors.

The book is organised into eleven chapters, covering (amongst other things) selectors, DOM traversal and manipulation, events, effects, and AJAX. The whole library is covered (as you would expect from a reference guide).

Each method in the library has its parameters documented. This is surprisingly helpful – many jQuery methods can be overloaded for various purposes, and this feature of the book is a great help towards learning the possibilities of each method. In fact, one of the take-home points for me was that many methods now support anonymous functions as parameters, offering lots of flexibility.

Next, a bare bones code sample is shown to demonstrate the method. It is followed  by an explanation of the code sample, along with any ‘gotchas’ or quirks that you might experience. Multiple examples of code are shown where there might be different ways to use a particular method. The book really is quite exhaustive, and from that point of view offers great value for money.

Although this book works really well as a reference book, the surprise for me was that it didn’t just work well as a reference book. Reading through the book, I picked up a lot of hints and tips that will almost certainly change how I use jQuery. For example, I realised that my knowledge of selectors was sorely lacking. (Partly this was because I’d never bothered to learn the CSS3 selectors properly, but jQuery gives me a reason!)

Another excellent chapter for me was the last one, regarding plug in architecture. The structure of this chapter was a little different, basically walking the reader through the process of creating various types of plugin. Like the best technical books, this chapter actually inspired me to put the book down and write some code – surely a sign that it was doing its job properly!

It’s worthwhile to note that if you are considering purchasing a copy of this book, Packt will make a contribution to the jQuery project for every copy sold; so in buying this book, you’ll increase your own knowledge, and you’ll be directly helping the jQuery project too. For me, that’s one of the best reasons to buy.

Posted in javascript, jquery, web development at March 24th, 2010. 1 Comment.

Anonymous functons with FireUnit

FireUnit makes it quick and easy to write unit tests in JavaScript, doing something like the following:

fireunit.ok(foo==bar, 'foo should equal bar');

But what if you need to do something a little more complex than a one liner? You can write and execute an anonymous function, like this:

fireunit.ok(function(){
    var myList = $('ul');
    var originalLen = myList.children('li').length;
    fireunit.click( myList );
    return $(myList).children() == originalLen + 1;
}(), 'This test should return true');

Two things to note here:

  1. Make sure that you include the extra parentheses after the function definition; this executes the function straight away
  2. Make sure you  return true or false  from the function (or an expression that evaluates to true or false.)
Posted in javascript, web development at March 22nd, 2010. 1 Comment.

Pro Javascript Techniques by John Resig – a review

pro-javascript-techniquesI’ve just finished reading Pro JavaScript Techniques by John Resig, and I thought I’d post some thoughts about it.

The author, John Resig, is the creator of the popular jQuery library. He also works as a Javascript evangelist for Mozilla, so there’s no doubt that he’s one of the best known proponents of the language.

Although I use the jQuery library on a daily basis, I was keen to brush up on some of the finer points of Javascript; my knowledge of the jQuery library probably exceeds my knowledge of Javascript! For this reason, I thought the book sounded good.

The first half of the book certainly doesn’t disappoint. Covering the nitty-gritty of DOM traversal, OO Javascript, and unobtrusive scripting, the book does a great job of covering a lot of ground in a concise, clear manner. The key concepts are illustrated with plenty of code snippets which do a great job of illuminating the subject matter.

The second part of the book was less useful for me, illustrating some examples of AJAX functionality, image galleries, autocomplete, that sort of thing. These topics might have been considered intermediate to advanced in 2006, when the book was released, but the plethora of options around today means that developing stuff like that now is re-inventing the wheel. (Of course you might be interested in learning more about wheels!)

You can almost see the snippets of code in this book forming the nuts and bolts of the jQuery library, and it’s interesting to take a look at the hoops we developers would have to jump through to otherwise gain cross-browser compliance.

I’m just glad I’m not the one having to negotiate those hoops myself!

To sum up, this book is still worth reading for the first half alone, and the stuff on OO javascript, scoping, closures etc is really useful. But some might consider the book a little out of date, so be warned.

Posted in AJAX, javascript, web development at October 3rd, 2009. No Comments.

Converting Prototype’s Ajax.PeriodicalUpdater to jQuery

One of the things I missed when switching from Prototype to jQuery was the former’s Ajax.PeriodicalUpdater function. It is used to provide a ‘decay’ mechanism for ajax calls, making them less and less frequent if the retrieved content doesn’t change inbetween calls.

It can be a seriously useful piece of functionality. I took a chatroom that was polling every second and using an entire CPU core (50% usage!) and reduced it to 2-3% using this method. There were also errors retrieving content because sometimes the responses would take longer than a second to come back!

The problem is, there really isn’t a similar piece of functionality in jQuery.

So here it is! The script checks the returned data against previously received data, and increases the time between calls if it hasn’t changed.

I’d be interested in your comments. Would it be useful to turn this into a plugin?

View the demonstration here.

Posted in AJAX, jquery, web development at February 18th, 2009. 8 Comments.

Using JSON to access the Twitter search API

Steve Reynolds recently wrote a blog post showing how to access the twitter search API using PHP, cURL, and JQuery.

Steve used JQuery to post to a page on his server, which then cURLed in search results for a given term. This approach is often necessary to get avoid the issue of cross domain ajax calls.

While this approach works well, there’s an even easier way to go about it – $.getJSON!

There are two main advantages to this approach:

  • Server side technology isn’t an issue. You don’t have to rely on PHP, cURL, firewalls, anything like that. It will even work on a static HTML page!
  • All the work is done on the client’s browser – saving precious bandwidth! This could be important on busy sites.

I’ve knocked up a quick-and-dirty demonstration of this concept. If a name doesn’t already exist for this methodology, my vote goes for JAJA (Javascript and JSON, asynchronous!)

View the demonstration here!

Posted in AJAX, jquery, web development at February 7th, 2009. 10 Comments.