2

Is onload equivalent to onreadystatechange with readyState 4, status 200?

This SO post says yes

But if they are equivalent, then why does this MDN article nest the two as such:

var xhr = new XMLHttpRequest();
xhr.open("GET", "/bar/foo.txt", true);
xhr.onload = function (e) {
  if (xhr.readyState === 4) {
    if (xhr.status === 200) {
      console.log(xhr.responseText);
    } else {
      console.error(xhr.statusText);
    }
  }
};
xhr.onerror = function (e) {
  console.error(xhr.statusText);
};
xhr.send(null);

[EDIT] Not duplicate of:

Is onload equal to readyState==4 in XMLHttpRequest?

The second answer in the above post explains that it is not exactly the same as readyState === 4. However, if you include status === 200 you have addressed the difference he/she talks about. My OP shows status nested inside readyState.

Magnus
  • 6,791
  • 8
  • 53
  • 84
  • 1
    Possible duplicate of [Is onload equal to readyState==4 in XMLHttpRequest?](https://stackoverflow.com/questions/9181090/is-onload-equal-to-readystate-4-in-xmlhttprequest) – bigless Jun 08 '18 at 18:28
  • @bigless Nope, not a duplicate. The second answer in the post you linked explains that it is not exactly the same as `readyState == 4`, but if you include `status == 200` you have addressed the difference he talks about. My OP shows `status` nested inside `readyState`. – Magnus Jun 08 '18 at 20:35
  • I believe that it is because he say that in case of onreadystatechange with readyState 4 can be onerror called too. onerror cannot happen together with onload so I think thats clear enough. – bigless Jun 09 '18 at 11:33
  • So to answer. status === 200 makes onload and onreadystatechange with readyState 4 equal. – bigless Jun 09 '18 at 11:40
  • @bigless But my question was: If they are equal, then why does MDN nest the two? I included all the info in my OP. – Magnus Jun 09 '18 at 12:49
  • 1
    My two cents: on MDN, it should be onreadystatechange. Not onload. – bigless Jun 09 '18 at 12:54

0 Answers0