7

I tested two kinds of doing XHR:

1.

xhr.onreadystatechange = function() {

            if (this.readyState == 4 && xhr.status !== 500) {

                function getElementByXpath(path) {

2.

xhr.onload= function() {

                function getElementByXpath(path) {

and don't realized any difference. Could somebody point me to it? Or is there realy no one?

1

1 Answer 1

9

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.

2
  • Does it mean the result of xhr.onLoad is always data, while the result of readystatechange could be, data, undefined and 0?
    – Evgeniy
    Commented Mar 15, 2020 at 20:49
  • 2
    No. You've confusing the readyState with the responseText.
    – Quentin
    Commented Mar 15, 2020 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.