Easiest way to retrieve cross-browser XmlHttpRequest Easiest way to retrieve cross-browser XmlHttpRequest ajax ajax

Easiest way to retrieve cross-browser XmlHttpRequest


While I would recommend using a full library to make usage easier, making AJAX requests can be fairly simple in modern browsers:

var req = new XMLHttpRequest();req.onreadystatechange = function(){    if(this.readyState == 4){        alert('Status code: ' + this.status);        // The response content is in this.responseText    }}req.open('GET', '/some-url', true);req.send();

The following snippet is a more advanced snippet based on a snippet from quirksmode.org and even supports very old browsers (older than Internet Explorer 7):

function sendRequest(url,callback,postData) {    var req = createXMLHTTPObject();    if (!req) return;    var method = (postData) ? "POST" : "GET";    req.open(method,url,true);    // Setting the user agent is not allowed in most modern browsers It was    // a requirement for some Internet Explorer versions a long time ago.    // There is no need for this header if you use Internet Explorer 7 or    // above (or any other browser)    // req.setRequestHeader('User-Agent','XMLHTTP/1.0');    if (postData)        req.setRequestHeader('Content-type','application/x-www-form-urlencoded');    req.onreadystatechange = function () {        if (req.readyState != 4) return;        if (req.status != 200 && req.status != 304) {//          alert('HTTP error ' + req.status);            return;        }        callback(req);    }    if (req.readyState == 4) return;    req.send(postData);}var XMLHttpFactories = [    function () {return new XMLHttpRequest()},    function () {return new ActiveXObject("Msxml3.XMLHTTP")},    function () {return new ActiveXObject("Msxml2.XMLHTTP.6.0")},    function () {return new ActiveXObject("Msxml2.XMLHTTP.3.0")},    function () {return new ActiveXObject("Msxml2.XMLHTTP")},    function () {return new ActiveXObject("Microsoft.XMLHTTP")}];function createXMLHTTPObject() {    var xmlhttp = false;    for (var i=0;i<XMLHttpFactories.length;i++) {        try {            xmlhttp = XMLHttpFactories[i]();        }        catch (e) {            continue;        }        break;    }    return xmlhttp;}


As requested, simple and proven to work:

function Xhr(){ /* returns cross-browser XMLHttpRequest, or null if unable */    try {        return new XMLHttpRequest();    }catch(e){}    try {        return new ActiveXObject("Msxml3.XMLHTTP");    }catch(e){}    try {        return new ActiveXObject("Msxml2.XMLHTTP.6.0");    }catch(e){}    try {        return new ActiveXObject("Msxml2.XMLHTTP.3.0");    }catch(e){}    try {        return new ActiveXObject("Msxml2.XMLHTTP");    }catch(e){}    try {        return new ActiveXObject("Microsoft.XMLHTTP");    }catch(e){}    return null;}

Collapsing it into a single line, we get:

function Xhr(){    try{return new XMLHttpRequest();}catch(e){}try{return new ActiveXObject("Msxml3.XMLHTTP");}catch(e){}try{return new ActiveXObject("Msxml2.XMLHTTP.6.0");}catch(e){}try{return new ActiveXObject("Msxml2.XMLHTTP.3.0");}catch(e){}try{return new ActiveXObject("Msxml2.XMLHTTP");}catch(e){}try{return new ActiveXObject("Microsoft.XMLHTTP");}catch(e){}return null;}


not 100% certain of your question - but if you're asking for function to return a cross browser XMLHTTP instance - we have used this in our native ajax library for years - and never a problem in any browser

function getXMLHTTP() {    var alerted;    var xmlhttp;    /*@cc_on @*/    /*@if (@_jscript_version >= 5)    // JScript gives us Conditional compilation, we can cope with old IE versions.    try {        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")    } catch (e) {    try {        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")    } catch (E) {        alert("You must have Microsofts XML parsers available")    }    }    @else        alert("You must have JScript version 5 or above.")        xmlhttp=false        alerted=true    @end @*/    if (!xmlhttp && !alerted) {        // Non ECMAScript Ed. 3 will error here (IE<5 ok), nothing I can        // realistically do about it, blame the w3c or ECMA for not        // having a working versioning capability in  <SCRIPT> or        // ECMAScript.        try {            xmlhttp = new XMLHttpRequest();        } catch (e) {            alert("You need a browser which supports an XMLHttpRequest Object")      }    }    return xmlhttp}