4
<div id="myDiv"></div>
<script>                    
    var xmlhttp = new XMLHttpRequest();
    document.getElementById("myDiv").innerHTML += xmlhttp.readyState + "<br/>";
    xmlhttp.onreadystatechange = function() {
        document.getElementById("myDiv").innerHTML += xmlhttp.readyState + "<br/>";
    }
    xmlhttp.open("GET", "example.com", true);
    xmlhttp.send();
</script>

I get this in display:

0
1
1
2
3
4

Why there are two 1s?

My browser is firefox 17.0.

Yishu Fang
  • 8,258
  • 13
  • 57
  • 93
  • [here](http://stackoverflow.com/a/632800/815386) explained statuses, seems firefox set status 1 on `send()` to inform js engine that request begins to be transferred, try to remove send() and see how much statuses you will receive – zb' Jan 22 '13 at 03:09
  • Interestingly when you pass false to `open()` it results in just one 1, but no 2 and 3. You can add this link to your question: http://jsfiddle.net/S55vq/1/ for convenience – Oleg Mikheev Jan 22 '13 at 03:23
  • also given that Chrome produces just single 1 it's just a browser specific behaviour, next release of FF might well produce single 1 too – Oleg Mikheev Jan 22 '13 at 03:38

1 Answers1

1

Ready states are well defined for XML HTTP object: https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#Properties.

But sequences of ready states seem to be different for every browser, so you can't rely on particular sequence of states in your code.

FF 19: 0, 1, 1, 2, 3, 4

Chrome 24: 0, 1, 2, 3, 4

Opera 12.12: 0, 1, 2, 3, 4

Safari 5.1: 0, 1, 2, 3, 4

IE 9: 0, 1, 1, 2, 3, 4

You can test your browser here.

Oleg Mikheev
  • 16,026
  • 12
  • 69
  • 93