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 : http://blogs.msdn.com/ie/archive/2006/01/23/516393.aspx
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 Object.prototype.toString.call() ?
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 thoughts on “IE + XMLHttpRequest problem and solution”

Comments are closed.