Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX readystatechange listener for IE7+8

(function (send) {

     XMLHttpRequest.prototype.send = function () {

         this.addEventListener('readystatechange', function() {

             console.log('readyState changed');

         }, false);

         send.apply(this, arguments);
     };

})(XMLHttpRequest.prototype.send);

This works, but it seems to be affecting other frameworks (like jQuery). When I use the code above, my jQuery ajax calls doen't complete (IE7 + IE8).

Is there any way to give the event a new alias so that it doesn't cause any conflicts (I'm just assuming that this is the problem)?

And I can't use this.onreadystatechange, since jQuery is overwriting it (see Fiddle).

As soon as I bind the event listener, the code seems to fail silently so I can't use the console to debug.

Fiddle

Compare in ie8 vs ie9 mode and notice that the console is empty in ie8

The reason why I need this is that I want a global ajax handler for another framework (GWT). I use jQuery just for testing it now.

Edit: Looks like only ie9 supports addEventListener. However, attachEvent doesn't seem to exist on the XHR object in ie7-8.

like image 654
Johan Avatar asked Apr 06 '13 09:04

Johan


1 Answers

We have had similar issue with jquery failing on IE8. I assume that you are making a cross domain request. Jquery does ajax through the XMLHttpRequest , but IE8 has replaced XMLHttpRequest object with XDomainRequest and jquery folks do not support it.So for IE8 , you will have to modify the code to use XDomainRequest with jquery.

see the links

Using jquery ajax for IE8

like image 66
Tito Avatar answered Oct 16 '22 06:10

Tito