HTML onstalled Attribute

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

Introduction

The onstalled attribute is an inline event handler that runs JavaScript when the stalled event fires on an audio or video element. It runs when the browser is trying to fetch media data but data is not arriving for an unexpectedly long time — often due to a slow or interrupted network connection during download or buffering. Use it to show a “connection problem” message, offer a retry button, or log analytics. Pair with onwaiting and onplaying for full buffering UX. Prefer addEventListener("stalled", …) in production.

What You’ll Learn

01

Event handler

Inline JS.

02

stalled

Fetch stuck.

03

audio / video

Media only.

04

vs onwaiting

Download vs play.

05

User message

Retry UX.

06

addEventListener

Preferred way.

Purpose of onstalled Attribute

The primary purpose of onstalled is to react when media data download stalls — so your page can inform the user, suggest checking their connection, or attempt recovery instead of leaving them with a frozen player and no explanation.

The stalled event fires on the media element when the user agent is actively trying to fetch data but none is forthcoming. It is related to network buffering issues, not the same as error (fatal load failure) or ended (playback finished).

💡
Not the same as onwaiting

onstalled means data download stopped unexpectedly. onwaiting means playback paused while waiting for more buffered data. Use both for complete buffering feedback.

📝 Syntax

Set onstalled on <audio> or <video>, or assign element.onstalled in JavaScript:

onstalled.html
<audio controls onstalled="handleStalled()">
  <source src="/audio/count.mp3" type="audio/mpeg">
</audio>

<script>
  media.onstalled = handleStalled;
  media.addEventListener("stalled", handleStalled);
</script>

Syntax Rules

  • Value is JavaScript executed when the stalled event fires.
  • Valid on <audio> and <video> elements.
  • Fires when media data fetch stalls — typically network-related.
  • May fire multiple times during a single load on poor connections.
  • Also assignable as element.onstalled in script.
  • Modern alternative: addEventListener("stalled", handler).
  • Use onerror for unrecoverable load failures.

💎 Values

The onstalled attribute accepts a string of JavaScript code:

  • onstalled="handleStalled()" — Call a named function.
  • onstalled="showRetryMessage()" — Show connection help text.
  • JavaScript: audio.onstalled = () => { … } — property assignment.
onstalled-handler.html
function handleStalled() {
  status.textContent = "Download stalled — check your connection";
  status.classList.add("is-stalled");
}

media.addEventListener("stalled", handleStalled);
media.addEventListener("playing", function () {
  status.textContent = "Playing";
  status.classList.remove("is-stalled");
});

⚡ Quick Reference

Property / APIWhen it runsNotes
stalledMedia fetch stops unexpectedlyonstalled
waitingPlayback paused for dataonwaiting
progressData downloadingonprogress
errorLoad failedonerror
networkStateRead anytimeNetwork status code

Applicable Elements

ElementSupported?Notes
<audio>YesRemote audio sources
<video>YesRemote video sources
<source>NoPut handler on parent media element
Local blob: URLsRareStalled usually needs network fetch
<iframe>NoEmbedded players are separate documents

onstalled vs onwaiting vs onprogress vs onerror

HandlerWhen it firesTypical use
onprogressData is downloadingBuffer progress bar
onstalledDownload unexpectedly stopsConnection warning, retry
onwaitingPlayback paused for bufferLoading spinner
onerrorMedia failed to loadError message, fallback

Examples Gallery

Inline audio handler, dynamic assignment, and stalled + playing status pair for buffering UX.

👀 Live Preview

Play the audio — status tracks stalled and playing events (throttle network in DevTools to test):

Ready — press play

Example — onstalled on audio

Run a function when media download stalls:

audio-onstalled.html
<audio controls onstalled="handleStalled()">
  <source src="/audio/count.mp3" type="audio/mpeg">
</audio>
<p id="status"></p>

<script>
  function handleStalled() {
    document.getElementById("status").textContent =
      "Audio loading has stalled!";
  }
</script>
Try It Yourself

How It Works

When the browser tries to fetch audio data but none arrives for an unexpectedly long time, the stalled event fires and runs handleStalled().

Dynamic Values with JavaScript

Assign the handler on an audio element at runtime:

dynamic-onstalled.html
<script>
  document.getElementById("dynamicAudio").onstalled = function () {
    log.textContent = "Media download stalled. Please try again.";
  };
</script>
Try It Yourself

How It Works

Assigning element.onstalled is equivalent to the inline attribute. Use this when you attach the handler after the DOM loads.

Example — Stalled and playing status

Clear stalled warning when playback resumes:

stalled-playing-pair.html
media.addEventListener("stalled", function () {
  status.textContent = "Download stalled";
  status.setAttribute("aria-busy", "true");
});

media.addEventListener("playing", function () {
  status.textContent = "Playing";
  status.removeAttribute("aria-busy");
});
Try It Yourself

How It Works

Stalled may be temporary. When data arrives and playback resumes, playing fires — reset your UI so users are not stuck on an old warning.

♿ Accessibility

  • Announce stalls — Use aria-live="polite" on status text when download stalls.
  • Offer retry — Provide a visible button to reload media, not only a console message.
  • Do not rely on color alone — Pair warning colors with clear text like “Connection problem.”
  • Keyboard access — Ensure native controls remain usable during stalls.
  • Captions — If video stalls, do not hide existing captions without reason.

🧠 How onstalled Works

1

Media loads / plays

Fetch begins.

Start
2

Data stops arriving

Network issue.

Stall
3

stalled fires

onstalled runs.

Event
=

Inform user

Message, retry, analytics.

Browser Support

The stalled event and onstalled handler are supported in all modern browsers on HTML5 audio and video elements.

HTML5 Media · Fully supported

Universal onstalled support

All major browsers fire stalled when media data fetch stops unexpectedly.

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
onstalled attribute 99% supported

Bottom line: Reliable for stall handling on HTML5 media in all modern browsers.

💡 Best Practices

✅ Do

  • Show a clear message when stalled fires
  • Pair with onplaying to clear warnings when data resumes
  • Use addEventListener("stalled", …) in production
  • Offer a retry or reload button for users
  • Test with DevTools network throttling

❌ Don’t

  • Confuse stalled with waiting or error
  • Show alert() on every stall
  • Assume one stall means permanent failure
  • Put onstalled on non-media elements
  • Use wrong MIME type (audio/mpeg not audio/mp3)

Conclusion

The onstalled attribute runs JavaScript when media data download stalls unexpectedly on audio or video — useful for connection warnings, retry buttons, and better buffering UX.

Pair it with onwaiting and onplaying, use addEventListener in production, and test on slow networks to verify your handlers behave correctly.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about onstalled

Bookmark these before wiring stall handlers.

5
Core concepts
▶️ 02

Media only

audio / video.

Scope
03

onwaiting

Playback pause.

Compare
🔄 04

+ onplaying

Clear warning.

Pair
05

Not onerror

May recover.

Gotcha

❓ Frequently Asked Questions

It runs JavaScript when the stalled event fires — when the browser is fetching media data but no data arrives for an unexpectedly long time.
onstalled means media data download stopped unexpectedly. onwaiting means playback paused while waiting for more buffered data.
Not necessarily. Stalls are often temporary. Use onerror for fatal failures and clear stall warnings on playing when data resumes.
Open browser DevTools, throttle the network to “Slow 3G,” then play remote audio or video. The stall may fire when data cannot keep up.
media.addEventListener("stalled", handler) is preferred in production — easier to attach multiple listeners and pair with other media events.
Yes in all modern browsers that support HTML5 audio and video.

Build resilient media players

Practice the onstalled attribute with connection warnings and recovery UX in the Try It editor.

Try audio stall example →

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