I'm trying to implement the XHR streaming Comet technique in jQuery, but am having some trouble detecting the onreadystatechange
event. I'm using Firefox 4.0.1.
I'm using prefilters (available since jQuery 1.5) to modify the native XHR object, specifically to add an event handler to the onreadystatechange
event. I've got a crude implementation of that at http://jsfiddle.net/Rdx6f/. That code appends to the document the XHR object's readyState
and responseText
attributes when the onreadystatechange
event is fired. Oddly, however, it only seems to detect the onreadystatechange
event when readyState
is 1.
In other words, jQuery seems to be detecting the onreadystatechange
event when the XMLHttpRequest object is in state "opened" (readyState 1), but not when the XHR object is in state "headers received" (readyState 2), "loading" (readyState 3), nor "done" (readyState 4).
Oddly, if I alert()
in the event handler, then I do see the onreadystatechange
handling when readyState is 1, 2, 3 and 4. See http://jsfiddle.net/Rdx6f/1/. Same code as http://jsfiddle.net/Rdx6f/, just alert()
ing instead of appending to the document.
I can also detect onreadystatechange
events when readyState
is 1, 2, 3 or 4 in native JavaScript -- see http://jsfiddle.net/d7vaH/. (That code is taken almost verbatim from Dylan Schiemann's implementation of XHR streaming on page 115 of the book "Even Faster Websites".)
Am I simply doing something wrong here, like using jQuery's Ajax prefilters wrong or making a JavaScript mistake? Is this perhaps a bug in jQuery?