diff --git a/files/en-us/web/api/xmlhttprequestupload/index.md b/files/en-us/web/api/xmlhttprequestupload/index.md index 1591b0c742aa325..4a83741ecbf4255 100644 --- a/files/en-us/web/api/xmlhttprequestupload/index.md +++ b/files/en-us/web/api/xmlhttprequestupload/index.md @@ -7,7 +7,7 @@ browser-compat: api.XMLHttpRequestUpload {{DefaultAPISidebar("XMLHttpRequest")}} -The **`XMLHttpRequestUpload`** interface represents the upload process, allowing to listen for events associated with a specific {{domxref("XMLHttpRequest")}}. It is an _opaque_ object that represents the underlying, browser-dependant, upload process. It is {{domxref("XMLHttpRequestEventTarget")}} and is can be obtained by calling {{domxref("XMLHttpRequest.upload")}}. +The **`XMLHttpRequestUpload`** interface represents the upload process, allowing it to listen for events associated with a specific {{domxref("XMLHttpRequest")}}. It is an _opaque_ object that represents the underlying, browser-dependant, upload process. It is {{domxref("XMLHttpRequestEventTarget")}} and is can be obtained by calling {{domxref("XMLHttpRequest.upload")}}. {{AvailableInWorkers("notservice")}} diff --git a/files/en-us/web/api/xmlhttprequestupload/readystatechange_event/index.md b/files/en-us/web/api/xmlhttprequestupload/readystatechange_event/index.md new file mode 100644 index 000000000000000..b0d5ccdb88fcd33 --- /dev/null +++ b/files/en-us/web/api/xmlhttprequestupload/readystatechange_event/index.md @@ -0,0 +1,62 @@ +--- +title: 'XMLHttpRequestUpload: readystatechange event' +slug: Web/API/XMLHttpRequestUpload/readystatechange_event +page-type: web-api-event +browser-compat: api.XMLHttpRequestUpload.readystatechange_event +--- + +{{APIRef}} + +The `readystatechange` event is fired whenever the {{domxref("XMLHttpRequest.readyState", "readyState")}} property of the {{domxref("XMLHttpRequest")}} changes. + +> **Warning:** This should not be used with synchronous requests and must +> not be used from native code. + +## Syntax + +Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}}, or set an event handler property. + +```js +addEventListener('readystatechange', (event) => { }) + +onreadystatechange = (event) => { } +``` + +## Event type + +A generic {{DOMxRef("Event")}} with no added properties. + +## Examples + +```js +const xhr = new XMLHttpRequest(); +const method = "GET"; +const url = "https://developer.mozilla.org/"; + +xhr.open(method, url, true); +xhr.onreadystatechange = () => { + // In local files, status is 0 upon success in Mozilla Firefox + if (xhr.readyState === XMLHttpRequest.DONE) { + const 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 + +{{Specifications}} + +## Browser compatibility + +{{Compat}} + +## See also + +- {{domxref("XMLHttpRequestUpload")}}