HTML onprogress Attribute

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 3 Examples
Events & Handlers

Introduction

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.

What You’ll Learn

01

Event handler

Inline JS.

02

progress

While loading.

03

audio / video

Media only.

04

.buffered

Bytes loaded.

05

+ canplaythrough

Loading done.

06

addEventListener

Preferred way.

Purpose of onprogress Attribute

The 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.

💡
Not the same as <progress>

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.

📝 Syntax

Set onprogress on audio or video, or assign media.onprogress in JavaScript:

onprogress.html
<audio controls preload="auto"
       onprogress="updateProgress(event)"
       oncanplaythrough="markReady()">
  <source src="/audio/count.mp3" type="audio/mpeg">
</audio>

<script>
  media.onprogress = updateProgress;
</script>

Syntax Rules

  • Value is JavaScript executed when the progress event fires.
  • Valid on <audio> and <video> elements.
  • Use preload="auto" or preload="metadata" so loading starts before play.
  • Read event.target.buffered to calculate loaded percentage.
  • Pair with oncanplaythrough to hide the loader when done.
  • Modern alternative: media.addEventListener("progress", handler).
  • Does not work on <img> — use media elements instead.

💎 Values

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.
  • JavaScript: audio.onprogress = (event) => { … } — property assignment.
onprogress-handler.html
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);

⚡ Quick Reference

Property / eventWhen it firesNotes
progressWhile media data downloadsonprogress
canplaythroughEnough buffered to play throughoncanplaythrough
waitingPlayback stalled (re-buffering)onwaiting
media.bufferedTimeRanges of loaded dataUse for percent
media.durationTotal media length (seconds)Denominator for %

Applicable Elements

ElementSupported?Notes
<audio>YesPrimary use case
<video>YesSame progress event
<img>NoDoes not fire progress reliably
<progress>NoDisplay element, not a media loader
XMLHttpRequest (JS)Relatedxhr.addEventListener("progress") — not HTML attribute

onprogress vs oncanplaythrough vs onwaiting

AttributeEventTypical use
onprogressprogressLoading bar, percent text
oncanplaythroughcanplaythroughHide loader, enable play
onwaitingwaitingSpinner during re-buffer
onloadeddataloadeddataFirst frame / sample ready

Examples Gallery

Inline audio handler, dynamic assignment, and a visual progress bar with addEventListener.

👀 Live Preview

Buffering status updates as this audio loads (reload the page to see it again):

Loading…

Example — onprogress on audio

Show buffering percentage while the file downloads:

audio-onprogress.html
<audio controls preload="auto"
       onprogress="updateProgress(event)"
       oncanplaythrough="markReady()">
  <source src="/audio/count.mp3" type="audio/mpeg">
</audio>
<p id="status">Loading…</p>
Try It Yourself

How It Works

The browser fires progress repeatedly as audio data arrives. Your handler reads buffered ranges to estimate how much is loaded.

Dynamic Values with JavaScript

Assign the handler on the media element at runtime:

dynamic-onprogress.html
<script>
  audio.onprogress = function (event) {
    var pct = bufferPercent(event.target);
    status.textContent = "Buffering… " + pct + "%";
  };
  audio.oncanplaythrough = markReady;
</script>
Try It Yourself

How It Works

Assigning audio.onprogress is equivalent to the inline attribute. Attach handlers after the element exists in the DOM.

Example — Visual progress bar

Update a bar width from addEventListener("progress"):

progress-bar.html
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";
});
Try It Yourself

How It Works

Each progress event gives you a chance to redraw the bar. When canplaythrough fires, set the bar to 100% and hide any loading overlay.

♿ Accessibility

  • Announce loading state — Put status text in an element with aria-live="polite" so screen readers hear progress updates.
  • Do not rely on color alone — Pair the progress bar with a text percentage.
  • Keep controls usable — Do not block native media controls while loading.
  • Avoid rapid announcements — Throttle live-region updates if progress fires very often.
  • Respect reduced motion — Skip animated spinners when prefers-reduced-motion is set.

🧠 How onprogress Works

1

Page loads media

Browser fetches file.

Download
2

progress fires

onprogress runs.

Repeat
3

Read buffered

Update UI %.

buffered
=

Ready to play

canplaythrough.

Browser Support

The progress event and onprogress handler on media elements are supported in all modern browsers that support HTML5 audio and video.

Media Events · Fully supported

Universal onprogress support

All major browsers fire progress while loading audio and video.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
Internet Explorer IE 9+ supported
Full support
Opera Fully supported
Full support
onprogress attribute 99% supported

Bottom line: Reliable for media loading feedback in all modern browsers.

💡 Best Practices

✅ Do

  • Use onprogress on audio / video
  • Read buffered + duration for percent
  • Pair with oncanplaythrough to finish loading UX
  • Use addEventListener("progress", …) in production
  • Add aria-live on loading status text

❌ Don’t

  • Put onprogress on <img> — it won’t work
  • Confuse <progress> element with the event
  • Run heavy logic on every progress tick
  • Assume event.loaded / event.total on media
  • Block playback controls during loading

Conclusion

The 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.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about onprogress

Bookmark these before adding loading indicators.

5
Core concepts
📈 02

While loading

Repeats.

Timing
🔎 03

.buffered

Get %.

API
04

+ canplaythrough

Done signal.

Pattern
05

Not on img

Wrong target.

Gotcha

❓ Frequently Asked Questions

It runs JavaScript when the progress event fires — periodically while the browser downloads and buffers media on audio or video elements.
No. Images do not reliably fire the progress event. Use onprogress on media elements, or XMLHttpRequest progress in JavaScript for downloads.
Read 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.
Yes in all modern browsers that support HTML5 audio and video.

Show loading progress clearly

Practice the onprogress attribute with buffering bars and status text in the Try It editor.

Try onprogress demo →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful