Is onload equal to readyState==4 in XMLHttpRequest?
It should be the same thing. onload
was added in XMLHttpRequest 2 whereas onreadystatechange
has been around since the original spec.
Difference between onreadystatechange and onload
A readystatechange
event fires every time the readyState changes (which is several times).
A load
event fires only when the request has completed successfully.
In your example you add some extra tests to your readystatechange
handler to test if it has reached the final state (4
(unless there are certain kinds of error in which case it will be 0
)) and to make sure it isn't a 500
error. There are other errors which would not trigger a load
event.
XMLHttpRequest readyState is equal to 0
xhrArrond.onreadystatechange = chargerArrondCallback(xhrArrond);
You are calling your event handler function and assigning its return value (undefined
) as the event handler.
Change it to:
xhrArrond.onreadystatechange = chargerArrondCallback;
… and then access the XHR object it is associated with using this
inside the function.
Can one replace xhr.onreadystatechange with xhr.onload for AJAX calls?
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 jquery or mootools that provides wrapping functionality for that.
Related Topics
Boolean Variable Returns as String from JavaScript Function
Access Object Properties Within Object
How to Redefine a JavaScript Class's Method
Automatic Semicolon Insertion & Return Statements
How to Flatten Nested Array in JavaScript
Extending Built-In Natives in Es6 with Babel
Angular2 Dynamic Input Field Lose Focus When Input Changes
Arrow Function in Object Literal
Destructuring and Rename Property
Difference and Intersection of Two Arrays Containing Objects
Iterate Over Object Literal Values
How to Return a JavaScript String from a Webassembly Function
Using Number as "Index" (JSON)
Jquery Difference Between Change and Click Event of Checkbox
How to Smoothly Scroll to an Element in Pure JavaScript