AJAX - The Complete Reference

(avery) #1

PART II


Chapter 5: Developing an Ajax Library 193


NNOT EOTE You might wonder why there’s an object inside of this property. The intention is later to add
more opportunities for visual updates. You’ll see these ideas in Chapter 8 when we explore Ajax
interface conventions.

A complete example that shows simple progress monitoring with various intervals and
visual displays can be found at http://ajaxref.com/ch5/simpleprogress.html and is shown
in Figure 5-1.

Request Control

As mentioned at the start of the library discussion, we made an assumption about cache
control and set the default to not disable any browser caching mechanism. However, given
Internet Explorer’s habit of caching GET requests, this might not be the desired action. It is
possible to override that with a header change. An easy way to do this is to simply set
preventCache to true in the options object, which will go ahead and use the XHR’s
setRequestHeader() method to set the If-Modified-Since header in the request to
some old date.

/* Prevent Caching if set */
if (request.preventCache)
request.xhr.setRequestHeader("If-Modified-Since", "Wed, 15 Nov 1995 04:58:08 GMT");

Beyond caching, it is very easy to set headers with the library. Given a simple options
object for a request such as:

var options = { method: "GET", outputTarget : "responseOutput" };

you could set a headers value as an array of headers to try to send. Here they are pushed
onto the array one at a time as a header name and value in an object format and then put in
the options object:

var headers = new Array();
headers.push({name: "X-factor", value: "true"});

FIGURE 5-1 Experiments with simple progress indications
Free download pdf