| title | ProgressEvent |
|---|---|
| slug | Web/API/ProgressEvent |
| page-type | web-api-interface |
| browser-compat | api.ProgressEvent |
{{APIRef("XMLHttpRequest API")}}{{AvailableInWorkers}}
The ProgressEvent interface represents events that measure the progress of an underlying process, like an HTTP request (e.g., an XMLHttpRequest, or the loading of the underlying resource of an {{HTMLElement("img")}}, {{HTMLElement("audio")}}, {{HTMLElement("video")}}, {{HTMLElement("style")}} or {{HTMLElement("link")}}).
{{InheritanceDiagram}}
- {{domxref("ProgressEvent.ProgressEvent", "ProgressEvent()")}}
- : Creates a
ProgressEventevent with the given parameters.
- : Creates a
Also inherits properties from its parent {{domxref("Event")}}.
- {{domxref("ProgressEvent.lengthComputable")}} {{ReadOnlyInline}}
- : A boolean flag indicating if the ratio between the size of the data already transmitted or processed (
loaded), and the total size of the data (total), is calculable. In other words, it tells if the progress is measurable or not.
- : A boolean flag indicating if the ratio between the size of the data already transmitted or processed (
- {{domxref("ProgressEvent.loaded")}} {{ReadOnlyInline}}
- : A number indicating the size of the data already transmitted or processed.
For a
ProgressEventdispatched by the browser in HTTP messages, the value refers to the size, in bytes, of the message body, excluding headers and other overhead. In compressed messages of unknown total size,loadedmight refer to the size of the compressed or uncompressed data, depending on the browser. As of 2024, it contains the size of the compressed data in Firefox, and the uncompressed data in Chrome. In aProgressEventyou create yourself, you can assign any numeric value toloadedthat represents the amount of work completed relative to thetotalvalue.
- : A number indicating the size of the data already transmitted or processed.
For a
- {{domxref("ProgressEvent.total")}} {{ReadOnlyInline}}
- : A number indicating the total size of the data being transmitted or processed.
For
ProgressEvents dispatched by the browser in HTTP messages, the value refers to the size, in bytes, of a resource and is derived from theContent-Lengthheader. In aProgressEventyou create yourself, you may wish to normalizetotalto a value such as100or1if revealing the precise amount of bytes of a resource is a concern. If using1as a total, for example, thenloadedwould be a decimal value between0and1.
- : A number indicating the total size of the data being transmitted or processed.
For
Inherits methods from its parent, {{domxref("Event")}}.
The following example adds a ProgressEvent to a new {{domxref("XMLHttpRequest")}} and uses it to display the status of the request.
const progressBar = document.getElementById("p"),
client = new XMLHttpRequest();
client.open("GET", "magical-unicorns");
client.onprogress = (pe) => {
if (pe.lengthComputable) {
progressBar.max = pe.total;
progressBar.value = pe.loaded;
}
};
client.onloadend = (pe) => {
progressBar.value = pe.loaded;
};
client.send();The total number of bytes of a resource may reveal too much information about a resource, so a number between 0 and 1 may be used in a {{domxref("ProgressEvent.ProgressEvent", "ProgressEvent()")}} instead:
function updateProgress(loaded, total) {
const progressEvent = new ProgressEvent("progress", {
lengthComputable: true,
loaded,
total,
});
document.dispatchEvent(progressEvent);
}
document.addEventListener("progress", (event) => {
console.log(`Progress: ${event.loaded}/${event.total}`);
});
updateProgress(0.123456, 1);{{Specifications}}
{{Compat}}
- The {{domxref("Event")}} base interface.