Will these ActiveXObject and XMLHttpRequest checks apply for any other browser than IE6?

倖福魔咒の 提交于 2019-12-24 02:09:05

问题


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;
    };
}

}


回答1:


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;
        }
    }
}


来源:https://stackoverflow.com/questions/14462286/will-these-activexobject-and-xmlhttprequest-checks-apply-for-any-other-browser-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!