56

I need to support major modern browsers only (IE10+, FF, Chrome, Safari)

Can I make this substitution as I want to simplify my code base:

From:

xhr.onreadystatechange = function () {
    if (this.readyState === 4) {
        if (this.status === 200) {
            o.callback(xhr.responseText);
        } else {
            return false;
        }
    } else {
        return false;
    }
};

To:

xhr.onload = function (test) {
    o.callback(xhr.responseText);
};

I don't feel that the MDN documentation is clear in this regard.

Clarification:

I choose not to use a framework.

informatik01
  • 16,038
  • 10
  • 74
  • 104
  • 1
    As they are they are functionally equivalent since you do nothing if the request fails. – Musa Feb 18 '13 at 22:17
  • 11
    @elias94xx "Trying it out" is not feasible in this case. One would have to try it out on all versions of all major browsers, and there would still be no guaranteed support. Furthermore some browsers may not actually support the specifications they claim to support so looking it up may not be reasonable here either. – Waleed Khan Feb 19 '13 at 03:10
  • 3
    possible duplicate of [Is onload equal to readyState==4 in XMLHttpRequest?](http://stackoverflow.com/questions/9181090/is-onload-equal-to-readystate-4-in-xmlhttprequest) – oHo Nov 07 '13 at 12:57
  • clarification = +1 m8 –  Dec 21 '15 at 00:13

4 Answers4

19

maybe you take a look at this one and a look at W3C: XMLHttpRequest it's the same if your browser supports xhr.onload. Requires XMLHttpRequest 2)

You can also write a wrapping function that emulates xhr.onload if it's not present. (I think you need to override XMLHttpRequest.prototype.onload = function(args){//calling onreadystatechanges somehow}). If you only support modern browsers using xhr.onload should be available - the best solution is using a framework (like or that provides wrapping functionality for that.

Community
  • 1
  • 1
mr.VVoo
  • 2,265
  • 20
  • 30
  • 7
    xhr2 is suported by IE10+ and of course the other 3. http://caniuse.com/#search=xhr2 –  Feb 19 '13 at 20:48
  • 3
    Update: it's 2017 - Frameworks are optional now that ES6 exists >_ – ChristoKiwi Mar 26 '17 at 23:41
  • If the two are equivalent, then why does this MDN article nest the two: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests – Magnus Jun 08 '18 at 17:25
16

In the MDN documentation they state the following

Events

onreadystatechange as a property on the xhr object is supported in all browsers.

Since then, a number of additional event handlers were implemented in various browsers (onload, onerror, onprogress, etc.). These are supported in Firefox. In particular, see nsIXMLHttpRequestEventTarget and Using XMLHttpRequest.

More recent browsers, including Firefox, also support listening to the XMLHttpRequest events via standard addEventListener APIs in addition to setting on* properties to a handler function.

So I think you can assume that onreadystatechange is the way to go and onload is a addition that can be used if the browser supports it. @mr.VVoo answer is the correct one, to stick to w3c when in doubt.

Gert Cuykens
  • 6,845
  • 13
  • 50
  • 84
4

Since the original asker said "I need to support major modern browsers only (IE10+, FF, Chrome, Safari)" it is obvious that onload, onerror, etc are the ways to go. Also, as of today, onreadystatechange is pretty much obsolete, as you are obviously not going to support anything that old that it supports onreadystatechange only.

To sum it up, forget about onreadystatechange.

Íhor Mé
  • 896
  • 9
  • 13
1

You can clean up your first example by doing this

xhr.onreadystatechange = function () {
    if (this.readyState === 4 && this.status === 200) {
        o.callback(xhr.responseText);
    } else {
        return false;
    }
};

Note, you probably want to check onload too with this.status === 200 if you are doing something with the else statement. Although, if you are checking for errors, there is also onerror, which you can write something like

xhr.onerror = function(){
    console.log('Error: Do something else...');
}
user126440
  • 391
  • 1
  • 2
  • 19