سؤال

I got a weird error in IE10 for plUpload plugin, and I found that if I remove this code in our project everything works fine. Can anyone tell me exactly what this does and if it's safe to remove? It looks like it only applies for IE6? Am I right?

var progids = ["Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
var progid = null;


if (typeof ActiveXObject != "undefined") {
var ie7xmlhttp = false;
if(typeof XMLHttpRequest == "object") {
    try {
        var o = new XMLHttpRequest();
        ie7xmlhttp = true;
    } catch (e) {
    }
}
if(typeof XMLHttpRequest == "undefined" || !ie7xmlhttp) {
    XMLHttpRequest = function() {
        var xmlHttp = null;
        if(!BlocAjax.noActiveX) {
            if(progid != null) {
                return new ActiveXObject(progid);
            }
            for(var i=0; i<progids.length && xmlHttp == null; i++) {
                try {
                    xmlHttp = new ActiveXObject(progids[i]);
                    progid = progids[i];

                }catch(e){}
            }
        }
        if(xmlHttp == null && MS.Browser.isIE) {
            return new .IFrameXmlHttp();
        }
        return xmlHttp;
    };
}

}

هل كانت مفيدة؟

المحلول

Yes, I believe there are other browsers. The checks you show are trying to detect IE by looking for browsers with ActiveX support (IE*), but no XMLHttpRequest support (IE6-). However if the ie7xmlhttp flag is presumably initialized to null or undefined, then any non-IE browser that doesn't have XMLHttpRequest support will be treated similarly since if(typeof XMLHttpRequest == "undefined" || !ie7xmlhttp) { will be true in those cases.

Thus, pretty much any older browser w/out XMLHttpRequest support will fall into the if block that tries to shim the XMLHttpRequest API. Not that there are a lot of people using them, but I'm sure they're out there. (e.g. particularly old versions of FF, Opera, Safari... lesser known mobile browsers maybe... that sort of thing.)

BTW, Microsoft's XMLHttpRequest documentation recommends this code snippet for x-platform XMLHttpRequest construction, which I recommend:

function getXMLHttpRequest() 
{
    if (window.XMLHttpRequest) {
        return new window.XMLHttpRequest;
    }
    else {
        try {
            return new ActiveXObject("MSXML2.XMLHTTP.3.0");
        }
        catch(ex) {
            return null;
        }
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top