Is Onload Equal to Readystate==4 in Xmlhttprequest

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



Leave a reply



Submit