readystatechange
Summary
Fires whenever the readyState of the request changes. Mostly used to determine whether the body of the response is available for handling.
Overview Table
Synchronous | No |
---|---|
Bubbles | No |
Target | apis/xhr/XMLHttpRequest |
Cancelable | No |
Default action |
The following script demonstrates how to set an asynchronous event handler that alerts the user when the readyState property of the request reaches complete (4
). Note that you must set the event handler after calling open, which resets all properties of the XMLHttpRequest object to their initial value.
function reportStatus() {
if (xhr.readyState == 4 /* complete */) {
if (xhr.status == 200 || xhr.status == 304) {
console.log("Transfer complete.");
}
else {
// error occurred
}
}
}
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://localhost/test.xml", true);
xhr.onreadystatechange = reportStatus;
xhr.send();
Related specifications
- W3C XMLHttpRequest Specification
- W3C Working Draft
Attributions
Microsoft Developer Network: Windows Internet Explorer API reference Article