IE + XMLHttpRequest problem and solution

I just found, yet another IE + JavaScript problem
First let me declare myself as NOT-IE-HATER. I do not hate or love any browser.
I just do not like to suffer consequences (daily) of bad software design decisions, or indecisions. So, this is one of them that (has not) made my day today.
It seems, bad decision was made here :
Explanation. Any IE javascript does this OK, (as per ES3 spec):

But … IE has ActiveX controls. And there is one which we have to use very often. XMLHttpRequest. Ok, so what is the problem, actually ?
This problem is very subtle, and as a such it can generate very subtle bugs.
The issue is that in IE (vs others) you can not call the XMLHttpRequest(). In IE it is of a type “object”.

And (lo and behold) this is a source of a lot of trouble and workarounds.
Especially in libraries and any( trying to be) generic javascript.

Has, someone forgot to handle “unknown” in ?
Why having “unknown” at all? Why not treat it as undefined, and no confusion:

Even better why not just making XMLHttpRequest a native global function ? As everybody else, as far as I know? And after all the same as IE’s own window.ActiveXObject() :

Since the proper cross-browser way (also for IE since IE7) is this :

Changing (in IE) XMLHttpRequest from object to function will brake no legacy code.
With that change the following will behave in IE as in any other ES3 compatible browser:

This change, if implemented will improve the consistency of IE scripting and will cost almost nothing.

3 Replies to “IE + XMLHttpRequest problem and solution”

  1. Of course above does not brake IE versions bellow 7. They have no global object or function called : XMLHttpRequest . Same applies for any other legacy browser “out there” and still in use.
    (WikiPedia article)

  2. browser do have cashing enable so it displays the old output if it gets same query string parameter. so solution for the problem is to try to send request with different query string parameter value each time….. below is the sample example
    [sourcecode lang=”js”]
    function FetchDGContents(SortField,SortType,hlp)
    //var selecteditem = document.Form1.ddlcity.value;
    //Starts displaying the Process Image table
    // = ‘visible’;

    var vReqid=Math.random() * 1000;
    var requestUrl = "/WebPF/HELP/Help.aspx?req="+(vReqid)+"&searchstr="+SortField+"&hlp="+SortType+"&fldname="+hlp;

    XmlReq.abort();"GET", requestUrl, false);
    XmlReq.onreadystatechange = HandleResponse;

  3. @kunal
    Er … sorry is Your comment for this or some other article ?
    Beyond that, the world is using jQuery, last few years or so, for these kind of issues :)

Comments are closed.