Page 1 of 1

[SOLVED]Quick ajax XMLHttpRequest referancing.

Posted: Wed Nov 01, 2006 8:58 pm
by JellyFish
Hey, I've been beginning to learn Ajax! And so far when setting a variable to reference the XMLHttpRequest, most tuts do something like:

Code: Select all

    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
id est, more typing then what I belive to be simple and cross-browser:

Code: Select all

var httpRequest;
(window.ActiveXObject) ? httpRequest = new ActiveXObject("Microsoft.XMLHTTP") : httpRequest = new XMLHttpRequest();
It seems to work well for me, just curious as to why most developers prefer the first solution rather then the second? Is it better coding habits? Do most developers prefer the first route? What do you prefer? And is it really cross-browser? Why not?

Post are appreciated.

Posted: Fri Nov 03, 2006 3:37 pm
by JellyFish
Nobody likes me? My post? :cry:

Posted: Fri Nov 03, 2006 3:40 pm
by Burrito
I've always used:

Code: Select all

if (window.XMLHttpRequest){
  reqsend = new XMLHttpRequest();
}else{
  reqsend = new ActiveXObject("Microsoft.XMLHTTP");
}
which is basically the same as your ternary. I really don't think it matters much, as long as the var for the object gets set.

Posted: Fri Nov 03, 2006 4:06 pm
by JellyFish
That's what I thought, just wondering why nobody uses the conditional operators that much. I guess it's a preference thing.

Thanks Burrito. That's basically all I wanted to know. :D

Re: [SOLVED]Quick ajax XMLHttpRequest referancing.

Posted: Mon Nov 06, 2006 3:46 pm
by Luke
If you really want the shortest route, you could shorten it a little further...

Code: Select all

var httpRequest = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();