AJAX - The Complete Reference

(avery) #1

Chapter 3: XMLHttpRequest Object 107


PART I


The results of this example are in Figure 3-9, which looks pretty much the same as the
previous example. To see if this technique works in your browser, try the example found at
http://ajaxref.com/ch3/partialreadystate.html.

NNOT EOTE Internet Explorer 7 and before cannot use the readyState to access partial data as it disallows
looking at responseText when you are in readyState 3 or before.

Other Firefox Event Handlers


Firefox also implements the onload and onerror event handlers for XHRs. The onload
handler is a convenience feature and corresponds to onreadystatechange reaching a
readyState value of 4. Given that most developers just use this readyState value, this is
an obvious change and certainly is a bit less cryptic than the integer codes. It also is
beneficial because you do not have to use closures to access the XHR object, which certainly
makes coding life more pleasant.
The onerror seems a promising feature as well and would be invoked when a network
error occurs. Unfortunately, this handler doesn’t seem to work properly yet and is poorly
documented. Until it is fixed, you will likely need to trap network errors using status codes,
creating timeouts, and using try-catch blocks as we have already alluded to in this
chapter; these will be presented in more depth in Chapter 6.

FIGURE 3-9
Partial data
consumption is
possible in some
browsers.
Free download pdf