[SOLVED]Quick ajax XMLHttpRequest referancing.

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

[SOLVED]Quick ajax XMLHttpRequest referancing.

Post 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.
Last edited by JellyFish on Fri Nov 03, 2006 4:07 pm, edited 1 time in total.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

Nobody likes me? My post? :cry:
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post 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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: [SOLVED]Quick ajax XMLHttpRequest referancing.

Post 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();
Post Reply