👀 Live Preview
Buffering status updates as this audio loads (reload the page to see it again):
Loading…

The onprogress attribute is an inline event handler for the progress event on audio or video elements. It runs periodically while the browser downloads and buffers media data — useful for showing a loading bar or “buffering…” message before playback can start smoothly. Read media.buffered and media.duration to estimate percent loaded. Pair with oncanplaythrough when loading finishes. Prefer addEventListener("progress", …) in production. Note: onprogress does not work reliably on <img> elements.
Inline JS.
While loading.
Media only.
Bytes loaded.
Loading done.
Preferred way.
onprogress AttributeThe primary purpose of onprogress is to give users real-time feedback while media is being fetched — updating a progress bar, showing a spinner, or displaying “Loading 45%…” before the file is ready to play without interruption.
The progress event fires on the media element as the browser downloads data. Unlike a one-time event such as canplaythrough, progress can fire many times as more bytes arrive. Use the buffered TimeRanges API to measure how much is loaded.
The HTML <progress> element displays a bar — onprogress is an event handler that runs while media loads. You often update a <progress> element from inside your handler.
Set onprogress on audio or video, or assign media.onprogress in JavaScript:
<audio controls preload="auto"
onprogress="updateProgress(event)"
oncanplaythrough="markReady()">
<source src="/audio/count.mp3" type="audio/mpeg">
</audio>
<script>
media.onprogress = updateProgress;
</script>progress event fires.<audio> and <video> elements.preload="auto" or preload="metadata" so loading starts before play.event.target.buffered to calculate loaded percentage.oncanplaythrough to hide the loader when done.media.addEventListener("progress", handler).<img> — use media elements instead.The onprogress attribute accepts a string of JavaScript code:
onprogress="updateProgress(event)" — Call a named function with the event.onprogress="status.textContent = 'Loading…'" — Inline statement.audio.onprogress = (event) => { … } — property assignment.function updateProgress(event) {
var media = event.target;
if (!media.duration || media.buffered.length === 0) return;
var end = media.buffered.end(media.buffered.length - 1);
var pct = Math.round((end / media.duration) * 100);
status.textContent = "Buffering… " + pct + "%";
}
audio.addEventListener("progress", updateProgress);| Property / event | When it fires | Notes |
|---|---|---|
progress | While media data downloads | onprogress |
canplaythrough | Enough buffered to play through | oncanplaythrough |
waiting | Playback stalled (re-buffering) | onwaiting |
media.buffered | TimeRanges of loaded data | Use for percent |
media.duration | Total media length (seconds) | Denominator for % |
| Element | Supported? | Notes |
|---|---|---|
<audio> | Yes | Primary use case |
<video> | Yes | Same progress event |
<img> | No | Does not fire progress reliably |
<progress> | No | Display element, not a media loader |
| XMLHttpRequest (JS) | Related | xhr.addEventListener("progress") — not HTML attribute |
onprogress vs oncanplaythrough vs onwaiting| Attribute | Event | Typical use |
|---|---|---|
onprogress | progress | Loading bar, percent text |
oncanplaythrough | canplaythrough | Hide loader, enable play |
onwaiting | waiting | Spinner during re-buffer |
onloadeddata | loadeddata | First frame / sample ready |
Inline audio handler, dynamic assignment, and a visual progress bar with addEventListener.
Buffering status updates as this audio loads (reload the page to see it again):
Loading…
Show buffering percentage while the file downloads:
<audio controls preload="auto"
onprogress="updateProgress(event)"
oncanplaythrough="markReady()">
<source src="/audio/count.mp3" type="audio/mpeg">
</audio>
<p id="status">Loading…</p>The browser fires progress repeatedly as audio data arrives. Your handler reads buffered ranges to estimate how much is loaded.
Assign the handler on the media element at runtime:
<script>
audio.onprogress = function (event) {
var pct = bufferPercent(event.target);
status.textContent = "Buffering… " + pct + "%";
};
audio.oncanplaythrough = markReady;
</script>Assigning audio.onprogress is equivalent to the inline attribute. Attach handlers after the element exists in the DOM.
Update a bar width from addEventListener("progress"):
media.addEventListener("progress", function () {
var pct = bufferPercent(media);
bar.style.width = pct + "%";
label.textContent = "Loading… " + pct + "%";
});
media.addEventListener("canplaythrough", function () {
bar.style.width = "100%";
label.textContent = "Ready to play";
});Each progress event gives you a chance to redraw the bar. When canplaythrough fires, set the bar to 100% and hide any loading overlay.
aria-live="polite" so screen readers hear progress updates.prefers-reduced-motion is set.Browser fetches file.
onprogress runs.
Update UI %.
canplaythrough.
The progress event and onprogress handler on media elements are supported in all modern browsers that support HTML5 audio and video.
onprogress supportAll major browsers fire progress while loading audio and video.
Bottom line: Reliable for media loading feedback in all modern browsers.
onprogress on audio / videobuffered + duration for percentoncanplaythrough to finish loading UXaddEventListener("progress", …) in productionaria-live on loading status textonprogress on <img> — it won’t work<progress> element with the eventevent.loaded / event.total on mediaThe onprogress attribute runs JavaScript while audio or video data is being downloaded — perfect for loading bars and buffering messages that keep users informed.
Use it on media elements, read the buffered API for percentages, pair with oncanplaythrough, and prefer addEventListener in production code.
onprogressBookmark these before adding loading indicators.
audio / video.
ScopeRepeats.
TimingGet %.
APIDone signal.
PatternWrong target.
Gotchaprogress event fires — periodically while the browser downloads and buffers media on audio or video elements.progress event. Use onprogress on media elements, or XMLHttpRequest progress in JavaScript for downloads.media.buffered.end() and divide by media.duration, then multiply by 100. Media progress events do not provide event.loaded / event.total like XMLHttpRequest does.onprogress fires during initial download. onwaiting fires when playback stalls and the player needs more data mid-playback.media.addEventListener("progress", handler) is preferred in production — multiple listeners and cleaner separation of concerns.Practice the onprogress attribute with buffering bars and status text in the Try It editor.
5 people found this page helpful