Getting started with jQuery's AJAX functionality

At the heart of jQuery's AJAX functionality is the .ajax() function. This little guy allows you to do some heavy lifting and has everything you need for all your XML HTTP Requests (XHR) needs.

For those of you with a little AJAX experience under your belts, you'll be pleased to find that in true jQuery form, this function eliminates the need for setting up the traditional if/else statement to test for support for the XMLHTTPRequest object and if not then, the ActiveXObject (for IE browsers).

Let's take a quick look at some of the functionality available in the .ajax call:

For example, implemented within WordPress, an .ajax() call might look something like this:

In the given code, when the user clicks on the .ajaxIt object jQuery selector, as seen in the next screenshot, the .ajax function loads the whole About page into the first post's .post div:

Using the .ajax() function

By changing the CSS properties on the div to hide the overflow and set the height, we can keep it from looking too messy:

Using the .ajax() function

There you have it! Your first use of AJAX within WordPress! However, you're probably thinking: "That's a fair bit of work for something that I'd never really want to do in real life. (Reloading in the whole site into a div including the header? Yuk!)"

You're right. Let's take a look at shortcutting-in some more accessible and useful functionality.

You can see the .ajax() function is quite robust and flexible. As cool as that is, you're probably already hoping for a shortcut. Never fear, similar to the .animate() function we've already worked with, jQuery has nicely broken down a few of the more "routine" tasks into bite size functions that are much easier to use and leverage. Here are the most important for WordPress users:

In most WordPress projects, you'll find that you won't need to use the .ajax() function at all. You'll use .load, .post or .get, sometimes .getJSON or .getScript. But, like the .animate() function, you'll occasionally come up with scenarios where the flexibility and granular control of the .ajax function is handy.

The most useful of all of these shortcut functions and the one we'll focus on the most is the .load function.