Skip to main content
WolfShade
Legend
June 1, 2017
Question

XMLHttpRequest.onreadystatechange - FireFox vs IE

  • June 1, 2017
  • 1 reply
  • 1552 views

Hello, all,

I'm posting this question here because I don't have an account on any other forums, and I don't know where else to go.

I have a .cfm page that is using Javascript XHR to post a single word value to itself which will determine how something is going to be processed.

PSEUDOCODE:

<div class="thisNav" id="getStats">Get User Stats</div><div class="thisNav" id="pruStats">Prune Stats (6 mos)</div><div class="thisNav" id="expStats">Export User Stats</div>

<script>

     var thisNav = [].slice.call(document.getElementsByClassName('thisNav')).map(function(d){

          d.onclick = function(event){getThis(d.id);};

          });

     xhr = new XMLHttpRequest();

     function getThis(act){

          xhr.open("POST","filename.cfm",true);

          xhr.setRequestHeader("Content-Type","x-www-form-urlencoded");

          xhr.onreadystatechange = function(){

               if(xhr.readyState*1 === 1){anotherDiv.innerHTML = " OPENING";}

               elseif(xhr.readyState*1 === 3){anotherDiv.innerHTML = " DISPLAYING";}

               elseif(xhr.readyState*1 === 4 && xhr.status === 200){anotherDiv.innerHTML = xhr.responseText;}

               }

          xhr.send("act=" + act);

          }

</script>

Now, in Internet Explorer 11, this works well (I know, I can't believe it, either.)

However, in FireFox 52, this will only display the last stage (xhr.responseText) on the first click (will not see readyState 1 or 3).  Subsequent clicks (if I don't refresh the page) will work as expected, but the initial click will not.

What is the difference between FF and IE that could cause this??  Is there some kind of fix or workaround??

V/r,

^_^

    This topic has been closed for replies.

    1 reply

    WolfShade
    WolfShadeAuthor
    Legend
    June 1, 2017

    I should add:  I've placed an alert(xhr.status) at the very beginning of the onreadystatechange.

    IE11 is displaying all of the alerts at the precise times.

    On the FIRST click, FF52 is only alerting status 4 and displaying the returned content.  So, for some reason, FF isn't seeing or reporting the first two status reports (1 and 3) and is apparently only seeing the last (status 4). Subsequent clicks without reloading/refreshing the page work as it does in IE11.

    Is this due to some kind of setting within FF options???  Or in about:config???

    V/r,

    ^_^

    BKBK
    Community Expert
    Community Expert
    June 3, 2017

    Does it help to do something like this?

    if(xhr.readyState == 1){anotherDiv.innerHTML = " OPENING";} 

                       else if(xhr.readyState == 3){anotherDiv.innerHTML = " DISPLAYING";} 

                       else if(xhr.readyState == 4 && xhr.status == 200){anotherDiv.innerHTML = xhr.responseText;}

    WolfShade
    WolfShadeAuthor
    Legend
    June 5, 2017

    Sorry.. since my dev environment is isolated from the internet, I had to manually type all that code, and neglected to place a space between else and if.  It is as you have suggested.

    V/r,

    ^_^