Best practice for AJAX / XmlHttpRequestSupport detection
I am trying to update an old JavaScript function used to detect AJAX support (i.e. XmlHttpRequest object). I have looked online (including SO) and found various solutions, but I'm not sure which is the most efficient one for simple support detection.
Current function:
    function IsSyncAJAXSupported()
    {
        var isSyncAJAXSupported = true;
        var xmlHttp = null;
        var clsids = ["Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
        for(var i=0; i<clsids.length && xmlHttp == null; i++) {
            try {
                    xmlHttp = new ActiveXObject(clsids[i]);
            } catch(e){}
        }
        if(xmlHttp == null && MS.Browser.isIE)
        {
            isSyncAJAXSupported = false;
        }
        return isSyncAJAXSupported;
    }
      
        
        
        
      
    In Firefox 3, the above gives errors because MS is undefined.
I understand that using the library would be better, but this is not a short term option. We only support IE6 and above + latest Firefox, Safari / WebKit and Opera.
What's the best way to get true / false support for XmlHttpRequest?
I came up with this:
var xhr = null;
try { xhr = new XMLHttpRequest(); } catch (e) {}
try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
return (xhr!=null);
      
        
        
        
      
    This seems to work, so I decided to share it.
Not
Or rather, don't waste time doing what many other people have done.
Try grabbing the source jQuery or somesuch and "borrow" their methods; they've already invested time in supporting as many browsers as possible (especially with jQuery), so save time.
NTN
My preferred code for this:
function CreateXMLHttpRequest()
{
  // Firefox and others
  try { return new XMLHttpRequest(); } catch (e) {}
  // Internet Explorer
  try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
  try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
  //alert("XMLHttpRequest not supported");
  // No luck!
  return null;
}
      
        
        
        
      
    You can easily add tests for variants of Microsoft objects ...