My JavaScript book is out! Don't miss the opportunity to upgrade your beginner or average dev skills.

Saturday, November 11, 2006

Which function to get an XMLHttpRequest object ?

Ajax is "quite old" but I've not found any crossbrowser clean or standard way to create and return the correct XMLHttpRequest.

I wrote a standard crossbrowser function in my italian Ajax Guide (http://javascript.html.it/guide/leggi/95/guida-ajax/) but someone, every day, try to discard that function or to create a "new way" to get that object.

My opinion is this one:
- You don't need any conditional comment, these aren't a good code practice
- You don't need any evil eval function ... You can do your check without problems
- You don't need any try catch, first because they're not compatible with old browsers, second because You can simply check browser version and then choose the correct ActiveX, if it's necessary


Then, if You're looking for a simple, fast and cross browser way to get XMLHttpRequest You could just use this function:


function xhr(){ // webreflection.blogspot.com
var xhr = null,
b = navigator.userAgent;
if(window.XMLHttpRequest)
xhr = new XMLHttpRequest();
else if(!/MSIE 4/i.test(b)) {
if(/MSIE 5/i.test(b))
xhr = new ActiveXObject("Microsoft.XMLHTTP");
else
xhr = new ActiveXObject("Msxml2.XMLHTTP");
};
return xhr;
};


Every compatible browser, FF 1+, Opera 8+, Safari 2+, KDE 3.4+, IE 5+, will get a valid XMLHttpRequest while every other browser, for example IE4, will recieve a null value.
Then You can simply verify if client browser support Ajax request using a sintax like this one:

var ajax = xhr();
if(ajax)
// do every async interaction ...


That's all, I hope this will be helpful :)

6 comments:

kentaromiura said...

look at the page that I linked,
the version independent is a alias to the 3.0 version so
"Msxml2.XMLHTTP.3.0","Microsoft.XMLHTTP" and "Msxml2.XMLHTTP"
are THE SAME object.
You may want to use
"Msxml2.XMLHTTP.6.0"
in different case because it is better.

Andrea Giammarchi said...

Msxml2.XMLHTTP.6.0 is supported by:
Windows 2000 Service Pack 4; Windows Server 2003; Windows Server 2003 Service Pack 1; Windows XP Service Pack 1; Windows XP Service Pack 2


Msxml2 is supported by every IE ... then I suppose this is the best solution, isn't it ?

However, You could check IE version (/MSIE 6/i.test(b)) and use Your favourite version without eval, try catch and without bad conditional comments too.

kentaromiura said...

"However, You could check IE version (/MSIE 6/i.test(b)) and use Your favourite version without eval, try catch and without bad conditional comments too."

ActiveX control is independent from IE version, you could have installed the 6.0 and use IE 5, (or not have IE at all) ;)
so if you look for IE 6 you are leaving out people that have installed the 6.0 but haven't IE6 .

Andrea Giammarchi said...

sure, and if you use try catch you are leaving out every people that have an old browser.

I don't like, really, try catch ... and I think version 6 without IE6 is a really particular case ...

Andrea Giammarchi said...

I don't like, really, try catch ... I mean not to get an XMLHttpRequest fake object .

Andrea Giammarchi said...

I forget to say that if application is not designed to be compatible with old browsers You could verify with try catch without problems.

Important thing is don't use eval or conditional comments.

I've seen your code and it seems to be good but not so good because You used @cc