Chapter 4: Serious Ajax Programming

In This Chapter

  • Handling multiple XMLHttpRequest requests

  • Using two XMLHttpRequest requests

  • Using an array of XMLHttpRequest requests

  • Using inner functions and multiple XMLHttpRequest requests

  • Handling JavaScript sent from the server

  • Overcoming browser caching

In Chapter 3 you got your feet wet with Ajax by writing complete Ajax applications. This chapter takes over from there, giving you a working knowledge of the skills you’ll need to work through the rest of this book. In other words, you’re going to see some serious Ajax here.

This chapter starts by working with multiple XMLHttpRequest objects. For example, if you present the user with two buttons, each of which downloads different data from the server, you can’t count on the user to not press both buttons and wait for the results from the server. That’s a problem, because Ajax is asynchronous, remember? It doesn’t block execution of your code until it gets a result from the server. So if you have only one XMLHttpRequest object, and each time the user clicks a button you connect an anonymous function to its onreadystatechange property, which XMLHttpRequest request are you responding to-the one from the first button click or the second button click? You’ll tackle that problem first in this chapter.

You’re also going to see that in addition to text and XML data, some Ajax applications return JavaScript for you to execute. That’s often the case when you connect to a large-scale, Ajax-based interface, such as Google Suggest. You can often connect to these applications using Ajax yourself, and a number of them return JavaScript for you to execute instead of simple text or XML data. (The JavaScript you get from Google Suggest displays the drop-down list of matches.) You’ll see how that works in this chapter.

Cross Ref 

For an example of Google Suggest at work, see the example in Chapter 1, which used Google Suggest to display a drop-down list of possible matches to a partial search term. You can also find Google Suggest at

In addition, you’re going to see how to use Ajax with HTTP head requests, allowing you to check whether files exist on the server, what their length and creation date is, and more. And you’ll also see how to defeat caching in browsers like Microsoft Internet Explorer, and many other topics as you start getting into serious Ajax programming.

Ajax Bible
Ajax Bible
ISBN: 0470102632
EAN: 2147483647
Year: 2004
Pages: 169 © 2008-2017.
If you may any questions please contact us: