h1

Ajax on IE7: make sure you invoke native XMLHttpRequest object

June 5th, 2006

Through working with the IE7 team, we’ve figured out the problem behind the IE7 XMLHttp resend issue I reported a while ago.

The short answer is this – we’ve demonstrated that in our app at least, IE7 resends its XMLHttpRequest data if you use the ActiveX XHR object, but works correctly if you use the native IE7 XHR object. Be sure that your XHR-creation code uses “new XMLHttpRequest()” to invoke the object in IE7 and you will not have the problem.

Tim Aiello explains in more detail.

5 comments to “Ajax on IE7: make sure you invoke native XMLHttpRequest object”

  1. AJAX Experience & Tools

    Hi there!   My name is Laurel Reitman and I’m a Lead Program Manager on the Internet Explorer…


  2. […] and what is wrong with this? Well, apart from being pretty verbose, in the world of IE7 we should be invoking XMLHttpRequest() first and having the ActiveX control done only if window.XMLHttpRequest isn’t around to play. […]


  3. […] Well, apart from being pretty verbose, in the world of IE7 we should be invoking XMLHttpRequest() first and having the ActiveX control done only if window.XMLHttpRequest isn’t around to play.         /*@cc_on         @if (@_jscript_version>= 5)                 try {                         xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");                 } catch (e) {                         try {                                 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");                         } catch (E) {                                 xmlhttp = false;                         }                 }         @else         xmlhttp = false;         @end @*/         if (!xmlhttp && typeof XMLHttpRequest != “undefined”) {                 try {                         xmlhttp = new XMLHttpRequest();                 } catch (e) {                         xmlhttp = false;                 }         }         return xmlhttp;   […]


  4. […] An XMLHTTPRequest tip Over on the Ajax Blog, Dion Almaer passed on an important tip from Brent Ashley and Tim Aiello for AJAX developers – to have your cross-browser AJAX work better with IE7, you really should be invoking the native XMLHTTPRequest (the cross-browser one) first to see if it’s available before instantiating the ActiveX control, instead of the other way around. In addition to the reasons that Brent and Tim discovered, I’ve seen a bunch of code that creates the XMLHTTPRequest object, uses it for a request, and then throws it away.  Obviously, this is a lot less performant than keeping the object around for multiple requests.  The native object’s lifetime can be as long as that of the page. So you can reuse it like this:             var  o = new XMLHttpRequestObject()            o.open(“GET”, “data1.xml”,  TRUE);            o.onreadystatechange = foo();            o.send();            …….            o.open(“GET”, “data2.xml”,  TRUE);            o.onreadystatechange=bar();            o.send(); Xmlhttp.open has a “reset” semantic so the second open() call on the same object will abort the previous connection, disconnect previous event handler, and reset the object. There’s also a handy tool by Julien Couvreur for debugging XHTMLHTTPRequest calls for IE, or you can use Fiddler. -Chris Published Thursday, June 08, 2006 3:30 PM by ieblog Filed Under: Tips and Tricks, Developers […]


  5. […] I’ve been making Ajax apps that work well enough for years while many people have been waiting for all the stars to align before they even try it. I’ve had all sorts of flack from pedants for using iframes and img/cookie because they’re hacks, but JSRS and RSLite have worked consistently and predictably across a large number of browsers for 5 years and more without modification. I only changed my Blogchat app to use XMLHttpRequest recently (for no really good reason – it’s been unchanged since 2002) and the first thing that happened was a huge debugging session to figure out a really wonky deep IE7 issue. […]