I am taking a crash course on java script for this course https://www.udacity.com/course/viewer#!/c-cs255/l-52473341/e-66738295/m-67610028, and I am really not sure what does an onload method of xmlHttprequest object does. I did google, but all the answers seems so advance and nothing relevant to the stuff I am learning. The course ended so no one will respond to my questions. Thanks,
1 Answers
onload
is not a method of the object, it's a property, like the onclick
property of a DOM element. You assign a function to this property, and the function will be called when the corresponding event occurs (e.g. you click on an A
element, and the onclick
handler function you assigned is called). This function is called the handler of the event.
The onload
handler of an XMLHttpRequest
object is called when the server responds to the AJAX request and the response has been parsed by the browser. It's more common to use the onreadystatechange
handler, though; this handler is called at various stages of processing of the AJAX request, and state 4
corresponds to the response being loaded. This is for historical reasons: onreadystatechange
was in the original version of the API, onload
was added later (see Is onload equal to readyState==4 in XMLHttpRequest?).