How to capture the status (and readyState) of the IE6 XHR?

Asked

Viewed 217 times

3

Like?

alert(new ActiveXObject("Microsoft.XMLHTTP").readyState);

// undefined

// 0

alert((new XMLHttpRequest).readyState);
// 0

Edit: in my code, before I had modified the this for xhr in the scope of onreadystatechange, but it hadn’t worked, and then I modified it again and it worked, when @bfavaretto and @Guilhermenascimento suggested it. I think I had forgotten something. That was actually a typo while I was modifying the code, but this should work (but not on some old browsers like IE6).

  • 6

    ie6 ? man, what freedom...

  • 1

    Could be that check of me.done, I don’t see this function being defined in your code.

  • @bfavaretto In fact the if declared before those 2 or executes, the this.readyState is undefined :( . He’s declared, but I put a /* ... */.

  • 1

    Will that this in IE is something else... It doesn’t hurt to try xhr.readyState...

  • @bfavaretto Also not... I put xhr and nothing happened :/. The this is the same as xhr in the scope of onreadystatechange.

  • 1

    Nicematt I know, should be, but in IE6, you never know ;-)

Show 1 more comment

2 answers

4


I’m not sure, but I suppose the problem is that due to "Ajax" in Internet Explorer 6, 7 and 8 (these last two if used in quirks-mode) use Activex and not XMLHttpRequest properly said it must make the Activex check "correct", in case it has the Microsoft.XMLHTTP and the Msxml2.XMLHTTP.

A very important detail is that you should always use XHR on pages http://, or be used in file:/// can fail.

So if you want to give some support for IE6 and 7/8 use so:

function XHR() {
    if (window.XMLHttpRequest) {
        return new window.XMLHttpRequest;
    } else if (window.ActiveXObject) {
        try {
            return new window.ActiveXObject("Msxml2.XMLHTTP");
        } catch (e1) {
            return false;
        }

        try {
            return new window.ActiveXObject("Microsoft.XMLHTTP");
        } catch (e1) {
            return false;
        }
     }
 }

A detail that in modern browsers does not affect, but in some ancient occurrences were the problem with the order of the methods call, so I always used the following orderm, .open, .onreadystatechange (or directly the .readyState) and .send.

The use of the code would be something like (read on the .readyState):

var foo = XHR();

foo.open("GET", "/url", true); //Usa chama assíncrona

//Use o readyState após o .open, como já dito
foo.onreadystatechange = function() {

    switch (foo.readyState) {
        case 0:
            //(não inicializado)
        break;
        case 1:
            //(carregando)
        break;
        case 2:
            //(já carregado)
        break;
        case 3:
            //(interativo)
        break;
        case 4:
            //(completo) ... Neste ponto já se pode chamar o responseText

            alert(foo.responseText);
        break;
    }
};

//Faz a requisição
foo.send(null);

An example with other Activex (however I developed a lot for Ies and the previous code worked since IE5.01+):

function XHR() {
    if (window.XMLHttpRequest) {
        return new window.XMLHttpRequest;
    }

    var XMLHttpFactories = [
        "Msxml3.XMLHTTP", "Msxml2.XMLHTTP", "Microsoft.XMLHTTP"
    ];

    var obj = false, i = 0, j = XMLHttpFactories.length;

    for (; i < j; i++) {

        try {
            obj = new window.ActiveXObject(XMLHttpFactories[i]);
        } catch (e) {
            continue;
        }

        break;
    }

    return obj;
}

Note: The window. is dispensable, I only used it to avoid conflicts with variables in other scopes, which may be almost impossible, but it is only to avoid.

A tip, prefer to avoid (never use) Sync mode:

foo.open("GET", "/url", false);
  • Thank you for the examples! It seems they still make the code work better, so the properties I use in normal XHR don’t exist yet... I think the name of these properties is really different! Because of this I can not callbacks in IE6. Obs: xhr.responseText also does not exist (even using msxml[3/2]).

  • @Nicematt to which properties do you refer? The use is almost equal as far as I have noticed, the only things I notice that are different is Activex for each browser version and order/moment that should be called the .readyState (mainly in old browsers).

  • @nicematt use xhr.responseText and xhr.responseText normally in IE5.01 and IE6 (I still have it installed for testing), it must be something you are getting confused about.

  • Yes, the properties readyState, status and responseText are undefined ! I think the problem is the program I’m using on Windows to test IE. Maybe it’s incomplete, right? Can you tell me the name of yours?

  • Note: I do the AJAX request the way you do your code.

  • @nicematt I have installed the IE5.01 and 6 "real" in a virtual machine, however I tested in Ietester and it worked normally too. You have tested the example I sent you, note that I edited the reply, add details about the open/readyState/send order and about when to use responseText ... Send me the name of the program you are using.

  • Your pure code worked. I think I must have a typo in mine. When I left only that "Microsoft.XMLHTTP" in the array XMLHttpFactories also worked. I had posted my code on the question before, but it seems that there was no problem... :o This looks like a magic! I’ll try to fit into that code now.

  • @nicematt once using event.keyCode, and always returned Undefined, but after one day I realized that I had written the C and minusculo: .keycode... PS: If you want to send the original code in the question, just for me to see, or using http://pastebin.com let me know and I see it.

  • All right... I’ll try to find the new bug after I’ve edited it, and I’ll send it to Pastebin.

  • 1

    @nicematt I found, and tested, the problem is that there were two situations, the first was the use of this that does not work for the ies and the second when used new ActiveXObject("Microsoft.XMLHTTP").readyState was due to the order of the methods call, which in the ies is necessary to use open/onreadystatechange/send.

  • Yes, I was doing in that order, and I had tested using xhr instead of this in the scope of onreadystatechange, but it also hadn’t worked... maybe this is related to the scope of my duties in exports.

  • 1

    @nicematt yes, I understand, but I meant the moment you used alert(new ActiveXObject("Microsoft.XMLHTTP").readyState);, but I think it’s already functional for the Ies :)

  • The strange thing is that it resulted in 0 when I executed that alert... I thought it would be undefined if I used my other code, but I did a test and gave 0 same. There’s a scope problem there for old browsers, ie I’ll have to make a change.

  • Looks like it was the this even the problem. I switched to xhr for the second time, and it worked, but the first time didn’t work. Thanks for the tips

Show 9 more comments

1

Notice that line:

var xhr = window.XMLHttpRequest ? new XMLHttpRequest :

Notice something wrong? You forgot the parentheses! The right thing would be this:

var xhr = window.XMLHttpRequest ? new XMLHttpRequest() :
  • 1

    Parentheses are optional with new. If you run new XMLHttpRequest in the console you will see a new instance. Anyway, thanks for the help! The error also cannot happen so the code performs correctly in IE6, so much so that it does even alerts in the event onreadystatechange.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.