AJAX - The Complete Reference

(avery) #1

88 Part I: Core Ideas


{
alert("Before open method: readyState: " + xhr.readyState );
xhr.open("GET",url,true);
xhr.onreadystatechange = function(){alert("In onreadystatechange
function: readyState: " + xhr.readyState);};
xhr.send(null);
}

The alert is useful as it blocks the progress of the request so you can watch the process
closely. However, if you want to see the progress of a request in a more real-time style, try
the example at http://ajaxref.com/ch3/readystate.html, which is displayed here:

Like many of the details of XHRs, readyState values can be a bit quirky depending
on the code and browser. For example, mysteriously, a readyState value of 2 may not be
seen in Opera browsers, at least in version 9 or before. Moving the position of the
onreadystatechange assignment, very different results will be experienced. Most of

readyState Value Meaning Description
0 Uninitialized The XHR has been instantiated, but the open()
method has not been called yet.
1 Open The XHR has been instantiated and the open()
method called, but send() has not been invoked.
2 Sent The send() method has been called, but no headers
or data have been received yet.
3 Receiving Some data has been received. Looking at headers or
content. This phase of loading may cause an error in
some browsers and not in others.
4 Loaded All the data has been received and can be looked at.
Note that the XHR may enter this state in abort and
error conditions.

TABLE 3-7 readyState Values
Free download pdf