AJAX Events (^) ❘ 209
}
$(this).attr('src', arrow);
}
}
);}
);This modifi cation looks like what you see in Figure 7-6, when you make an AJAX request. Because
you’re requesting a fi le from your own local computer, the activity indicator will be revealed and
hidden almost instantaneously. So this technique is obviously better suited for requesting content
from a remote server where there may be some latency.
FIGURE 7-6
In the JavaScript, you make a call to $.ajaxSetup() to defi ne events called beforeSend, success, and
error. Each of these events is defi ned inside a JavaScript object literal that is passed to the $.ajax-
Setup() method. By attaching a callback function to the beforeSend property, you are telling jQuery
to execute the specifi ed function before every AJAX request. In this case, you cause the activity
indicator to be displayed by calling jQuery’s show() method. Then, after the request has completed
successfully, you hide the activity indicator by attaching a callback function to the success and
error events, which, in turn, is executed upon a successful or failed request. (Another way to do this
is to attach to the AJAX complete event, which executes when a request is completed, whether it was
successful or failed.) These are but a few of the properties that you can specify using this method to
defi ne AJAX defaults globally for jQuery. All the options that can be specifi ed here are outlined in
detail in Appendix G.
You are not, of course, limited to this use of jQuery’s AJAX events. jQuery’s AJAX events can
also modify the HTTP headers that will be used in the request or to do other low-level things with
jQuery’s AJAX API.