An EventHandler
that is called whenever the readyState
attribute changes. The callback is called from the user interface thread. The XMLHttpRequest.onreadystatechange
property contains the event handler to be called when the readystatechange
event is fired, that is every time the readyState
property of the XMLHttpRequest
changes.
Warning: This should not be used with synchronous requests and must not be used from native code.
Syntax
XMLHttpRequest.onreadystatechange = callback;
Values
callback
is the function to be executed when thereadyState
changes.
Examples
const xhr = new XMLHttpRequest(),
method = "GET",
url = "https://developer.mozilla.org/";
xhr.open(method, url, true);
xhr.onreadystatechange = function () {
// In local files, status is 0 upon success in Mozilla Firefox
if(xhr.readyState === XMLHttpRequest.DONE) {
var status = xhr.status;
if (status === 0 || (status >= 200 && status < 400)) {
// The request has been completed successfully
console.log(xhr.responseText);
} else {
// Oh no! There has been an error with the request!
}
}
};
xhr.send();
Specifications
Specification | Status | Comment |
---|---|---|
XMLHttpRequest | Living Standard | WHATWG living standard |
Browser compatibility
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
onreadystatechange
|
Chrome
Full support 1 |
Edge
Full support 12 |
Firefox
Full support 1 |
IE
Full support 5 |
Opera
Full support 9 |
Safari
Full support 1.2 |
WebView Android
Full support 1 |
Chrome Android
Full support 18 |
Firefox Android
Full support 4 |
Opera Android
Full support 10.1 |
Safari iOS
Full support 1 |
Samsung Internet Android
Full support 1.0 |
Legend
- Full support
- Full support
XMLHttpRequest.onreadystatechange by Mozilla Contributors is licensed under CC-BY-SA 2.5.