02.06.2013 Views

JavaScript & jQuery: The Missing Manual ... - Robert Guajardo

JavaScript & jQuery: The Missing Manual ... - Robert Guajardo

JavaScript & jQuery: The Missing Manual ... - Robert Guajardo

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

In addition, most of the time, you’ll receive a text response, which is stored<br />

in the XHR object’s responseText property. This response could be a chunk of<br />

HTML, a simple text message, or a complex set of JSON data (see page 370).<br />

Finally, if the server responds with an XML file, it’s stored in the XHR object’s<br />

responseXML property. Although XML is still used, it’s more common for program<br />

server pages to return text, HTML, or JSON data, so you may never have<br />

a need to process an XML response.<br />

Whatever data the server returns, it’s available to the callback function to use<br />

to update the web page. Once the callback function finishes up, the entire Ajax<br />

cycle is over. (However, you may have multiple Ajax requests shooting off at the<br />

same time.)<br />

Ajax the <strong>jQuery</strong> Way<br />

<strong>The</strong>re are enough differences between browsers that you have to write extra code for<br />

your Ajax programs to work in Internet Explorer, Firefox, Safari, and Opera. And<br />

although the basic XMLHttpRequest process isn’t too complicated, since you must<br />

take so many steps each time you make an XHR request, your Ajax programming<br />

will go faster if you turn to a <strong>JavaScript</strong> library.<br />

<strong>The</strong> <strong>jQuery</strong> library provides several functions that greatly simplify the entire process.<br />

After all, if you look at the five steps in an Ajax request (page 347), you’ll see<br />

that the interesting stuff—the programming that actually does something with the<br />

server’s response—happens in just a single step (step 3). <strong>jQuery</strong> simplifies all of the<br />

other steps so you can concentrate on the really fun programming.<br />

Using the load() Function<br />

<strong>The</strong> simplest Ajax function offered by <strong>jQuery</strong> is load(). This function loads an<br />

HTML file into a specified element on the page. For example, say you have an area<br />

of a web page dedicated to a short list of news headlines. When the page loads, the<br />

five most recent news stories appear. You may want to add a few links that let visitors<br />

choose what type of news stories are displayed in this area of the page: for example,<br />

yesterday’s news, local news, sports news, and so on. You can do this by linking to<br />

separate web pages, each of which contain the proper news items—but that would<br />

force your visitors to move onto another web page (and wouldn’t use Ajax at all!).<br />

Another approach would be to simply load the selected news stories into the news<br />

headlines box on the page. In other words, each time a visitor clicks a different news<br />

category, the web browser requests a new HTML file from the server, and then<br />

places that HTML into the headlines box—without leaving the current page (see<br />

Figure 11-3).<br />

www.it-ebooks.info<br />

chapter 11: introducing ajax<br />

Ajax the <strong>jQuery</strong> Way<br />

349

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!