What’s New in jQuery 1.5
The jQuery team today announced the release, right on schedule, of version 1.5 of (almost) everybody’s favorite JavaScript library.
Ajax
The biggest change to the codebase in jQuery 1.5 is a complete rewrite of the Ajax module. A lot of what’s been done is mostly transparent to everyday developers, but there are some significant bits it’s worth being aware of.
Firstly, all Ajax calls (via the $.ajax()
method) now return a jqXHR object instead of the browser-native XMLHttpRequest. Primarily, the new object’s function is to wrap browser implementations of XMLHttpRequest to give you a nice, standardized interface to work with. But beyond that, it has another fantastic trick up its sleeve. The jqXHR object implements a new interface called a Deferred object. The Deferred object is a cool construct that handles callback queues much more cleanly than what $.ajax()
did in the past. You can add as many callbacks as you like to a Deferred object, and they’re all placed into a tidy queue to be executed when the Ajax call completes. But what if you add more callbacks after the call has already completed? No sweat. jQuery knows what’s up, so it will just run your callbacks as required anyway.
What this means is that you make your Ajax call, and you get back a jqXHR object. You can chain your callbacks to that object straight away, like this:
var jqxhr = $.ajax({ url: "example.php" })
.success(function() { alert("that went well"); })
.error(function() { alert("ack! an error!"); });
You can add as many callbacks of each type to the request as you’d like. Even better, because you’ve got this Deferred object, you can add more callbacks to it at any time, potentially much later in your code:
jqxhr.error(function() { alert("did you hear me? there was an error!") });
If the request has already returned an error by the time you assign that extra callback, your extra code will still be run.
This new functionality is even useful beyond the realm of Ajax: because Deferred is a generic utility object, you can use it yourself for any of your code that requires a bunch of callbacks to be managed transparently. Pretty slick. For more on using Deferred objects, the announcement post on the jQuery blog recommends this post by Eric Hynds, which offers a good in-depth coverage of the topic.
The other major addition to the Ajax API is a set of methods for extending the base functionality. These will be of particular interest to plugin developers, but just about anyone using more than the basic Ajax methods might be able to gain something from the ability to make across-the-board customizations. Check out the details on the Extending Ajax page in the API docs.
Performance
The jQuery team has also managed to significantly improve the performance of several of the library’s DOM traversal methods, namely prev()
, next()
, and children()
. The improvements range from huge to massive, depending on the browser being used. To take the most striking example, most versions of Chrome have gone from performing a little over a thousand children()
operations per second in jQuery 1.4 to around 20,000 in jQuery 1.5.
The announcement blog post has performance comparison graphs, so if you like some eye candy (and we all do) be sure to check it out.
Bug Fixes
Of course, those are just the big changes. As with any major release of an open-source library, jQuery 1.5 includes a slew of bug fixes, which you can browse through here.
Get Cracking!
So there you have it! Another year, another jQuery. Check out the full release notes for a more detailed description of what’s new, then fire up your editor of choice and start putting it to use in your projects. Happy coding!