How to detect if the browser accepts xmlhttprequest events?

1

It's the following, darned, to make my object compatible with older browsers (if not scold me) I've created a new problem. It is difficult to divert the path according to the browser version because it was not so standardized, browsers accepted one feature and another did not, so I did the following:

var reqEngatilhar = function(){
    este.concluido = false;
    timeoutId = setTimeout(reqTimeout, este.timeout);
    if(este.Request.hasOwnProperty("onload")){
        este.Request.addEventListener("error", reqErro, true);
        este.Request.addEventListener("progress", reqMon, false);
        este.Request.addEventListener("abort", reqAbort, false);
        este.Request.addEventListener("load", reqFim, false);
        console.log("$Http reqEngatilhar usando eventos...");
    } else {
        este.Request.onreadystatechange = function (e) { reqSwitch(e); };
        console.log("$Http reqEngatilhar usando onreadystatechange...");
    }
}

This function sets the events inside my object when it changes the request state xmlhttprequest (where this = this out of function). But some browsers do not accept events via addEventListener, so it defines onreadystatechange that will do the "distribution" by calling the right function.

The problem is the test.Request.hasOwnProperty ("onload") that is working only for Safari (my site only opens in Safari kkkkk).

So I ask: what is the correct (or best possible) way to detect if the browser accepts xmlhttprequest events via addEventListener?

    
asked by anonymous 18.06.2016 / 22:02

1 answer

0

Do this:

if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    try {
        req = new XMLHttpRequest();
    } catch(e) {
        req = false;
    }
} else if(window.ActiveXObject) { // Se for IE usa o control ActiveX
    try {
        req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
        try {
            req = new ActiveXObject("Microsoft.XMLHTTP");
        } catch(e) {
            req = false;
        }
    }
}
return req;

Full reference: link

    
19.06.2016 / 00:53