Affects: IE6, IE7, IE8
What!? you say? "They added this in IE7!" - Well yes, no, not quite!
Microsoft did add a non-ActiveX version of XMLHttpRequest ("XHR") so that if a user had turned off ActiveX for security reasons, AJAX would still work (this was a good thing).
However there is a key feature of JavaScript Objects that makes them so useful is that you can Prototype on them to add properties, methods, expando properties, or even subclasses. In addition it doesn't extend from Object, thus any prototyping done there won't be available on XHR, and none of the methods on XHR are exposed.
In IE6, the XHR Object was an ActiveX control only, in IE7 they added a "native" one, but it was just access to the XHR Object that didn't require ActiveX. In IE8 Beta 1 there are no changes, but In IE8 RTM, will we see a true native XMLHTTPRequest Object?
Example:
Lets say you wanted to improve the user experience when an AJAX request is sent to the server.
- You might want to set a timeout that a response is expected by, and if it isn't cleared, automatically call an error handler.
- You might want to queue up/discard duplicate requests if the browser has already sent a request in, and is waiting for a response.
- You might want to add some debug capabilities to trace the progress of your AJAX Requests
- Since an AJAX request might be very expensive on your server, you might want to add the ability to pass in a "cancellation" request, that is "tied" to the original request... this way if the user closes a window, or runs another query, you could send a quick "one-way" request to the server to cancel the first request.
In any of these cases, being able to store applicable info on the XHR object is ideal, and adding methods would enable callbacks to be self-contained.
<script type="text/javascript">
var myXHR = new XMLHttpRequest();
myXHR.lastAccess = null;
myXHR.requestTimeout = 15000;
myXHR.requestTimeoutHandler = myTimoutHandler;
</script>
The above will fail in IE, since expando's and prototypes are not an option.
Want to see what Properties/Methods are exposed on XMLHttpRequest in your browser?
Click the button to find out! (check in other browsers too)
(PS If you subscribe to this Blog via RSS/Atom, please try the button on the Blog site as we needed to update the sample several times to overcome some Blogger auto-line-break issues. - thanks)
Known Workarounds: None.
Related Issues: (bug 186), (bug 181).
Submit a bug