HTML onloadeddata Attribute

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

Introduction

The onloadeddata attribute is an inline event handler for the loadeddata event on media elements<audio> and <video>. It runs when the first frame of media data at the current playback position is loaded and ready to render. Use it to hide loading spinners, enable play buttons, or show “ready to play” messages. This is not the same as onload on the page or an image — those are different events. Prefer addEventListener("loadeddata", …) in production; inline onloadeddata is fine for learning.

What You’ll Learn

01

Event handler

Inline JS.

02

loadeddata

Media ready.

03

audio / video

Media only.

04

vs onload

Not page load.

05

addEventListener

Preferred way.

06

Media timeline

Event order.

Purpose of onloadeddata Attribute

The primary purpose of onloadeddata is to react when an <audio> or <video> element has enough media data loaded to display or play the current frame. That is the moment you can safely hide a “Loading…” overlay, show duration from metadata (often already available), or enable custom controls.

Media elements fire a sequence of events: loadstartloadedmetadataloadeddatacanplayplaying. The loadeddata event sits after metadata is known and before playback is guaranteed to be smooth (canplaythrough).

💡
Not for the whole page

body onload waits for the document and subresources. onloadeddata is only for audio and video media loading.

📝 Syntax

Set onloadeddata on an audio or video element:

onloadeddata.html
<audio controls onloadeddata="handleLoadedData()">
  <source src="track.mp3" type="audio/mpeg">
</audio>

<video controls src="clip.mp4" onloadeddata="videoReady()"></video>

Syntax Rules

  • Value is JavaScript executed when the loadeddata event fires.
  • Works on <audio> and <video> only.
  • Fires when media data for the current frame is available — not when the entire file is buffered.
  • May fire again if the user seeks to an unloaded portion of the media.
  • Pair with onerror if loading fails.
  • Modern alternative: media.addEventListener("loadeddata", handler).
  • Use correct MIME type on <source type> (e.g. audio/mpeg, not audio/mp3).

💎 Values

The onloadeddata attribute accepts a string of JavaScript code:

  • onloadeddata="handleLoadedData()" — Call a named function.
  • onloadeddata="this.classList.add('ready')" — Inline statement on the element.
  • JavaScript: video.onloadeddata = () => { … } assigns the handler.
onloadeddata-js.html
const player = document.getElementById("myVideo");

player.addEventListener("loadeddata", () => {
  document.getElementById("loader").hidden = true;
  document.getElementById("status").textContent = "Media data loaded — ready.";
});

⚡ Quick Reference

Event / handlerWhen it firesElement
onloadPage or image fully loadedbody, img, iframe
onloadedmetadataDuration, dimensions knownaudio, video
onloadeddataFirst frame data readyaudio, video
oncanplayCan start playbackaudio, video
onplayingPlayback actually startedaudio, video
Handler attributeonloadeddataMedia elements

Applicable Elements

TargetSupported?Notes
<audio>YesPrimary use case
<video>YesHide poster loader
<body> / windowNoUse onload instead
<img>NoUse img onload
<source> aloneNoAttach handler on parent audio/video

onloadeddata vs onloadedmetadata vs oncanplay

HandlerWhen it firesTypical use
onloadedmetadataMetadata parsed (duration, size)Show duration label early
onloadeddataFrame data at current time loadedHide spinner, show player
oncanplayEnough data to start playingEnable play button

Examples Gallery

Audio loadeddata handler, addEventListener on video, and hide a loading message.

👀 Live Preview

When the audio below loads media data, the status updates:

Waiting for loadeddata…

Example — Audio onloadeddata

Run a function when audio media data is loaded:

audio-onloadeddata.html
<audio controls onloadeddata="handleLoadedData()">
  <source src="track.mp3" type="audio/mpeg">
  Your browser does not support the audio element.
</audio>
<p id="status"></p>

<script>
  function handleLoadedData() {
    document.getElementById("status").textContent =
      "Audio data has been loaded!";
  }
</script>
Try It Yourself

How It Works

The browser downloads the audio, parses metadata, then fires loadeddata when the first frame of media is available at the current time.

Dynamic Values with JavaScript

Attach the handler with addEventListener:

dynamic-onloadeddata.html
<video id="dynamicMedia" controls src="clip.mp4"></video>

<script>
  document.getElementById("dynamicMedia").addEventListener("loadeddata", function () {
    console.log("Media data has been loaded!");
  });
</script>
Try It Yourself

How It Works

Register once in script. The callback runs when the media element reaches the loadeddata state.

Example — Hide loading spinner

Reveal the player when media data is ready:

spinner-onloadeddata.html
const audio = document.getElementById("player");
const loader = document.getElementById("loader");

audio.addEventListener("loadeddata", () => {
  loader.hidden = true;
  audio.hidden = false;
});
Try It Yourself

How It Works

Users see feedback while media buffers; loadeddata is a good point to reveal controls without waiting for the entire file.

♿ Accessibility

  • Provide native controls — Use controls or accessible custom controls with labels.
  • Announce ready state — Use aria-busy="false" when loading finishes.
  • Do not autoplay with sound — Respect user preferences; use muted if autoplay is required.
  • Captions for videoloadeddata does not replace <track kind="captions">.
  • Loading feedback — Visible or screen-reader text while media loads helps all users.

🧠 How onloadeddata Works

1

Browser fetches media

loadstart begins.

Network
2

Metadata then data

loadedmetadata first.

Parse
3

loadeddata fires

onloadeddata runs.

Event
=

Player ready

Show UI, enable play.

Browser Support

The loadeddata event and onloadeddata handler are supported wherever HTML5 audio and video are supported — all modern browsers.

Media Events · Fully supported

Universal onloadeddata support

All major browsers fire loadeddata on audio and video elements.

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

Bottom line: Safe for audio/video loading UX in all browsers you target today.

💡 Best Practices

✅ Do

  • Use onloadeddata on audio and video only
  • Prefer addEventListener("loadeddata", …) in production
  • Pair with onerror for failed media loads
  • Use correct type on <source> (e.g. audio/mpeg)
  • Hide loaders and set aria-busy="false" when data is ready

❌ Don’t

  • Confuse onloadeddata with body onload
  • Attach handlers to bare <source> — use parent media element
  • Assume the entire file is buffered — only current frame data is guaranteed
  • Use invalid MIME types like audio/mp3
  • Autoplay loud media without user consent

Conclusion

The onloadeddata attribute runs JavaScript when an audio or video element has loaded media frame data — the right hook for spinners, ready messages, and custom players.

Prefer addEventListener("loadeddata", …), know the media event timeline, and pair with onerror for robust playback UX.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about onloadeddata

Bookmark these before building audio and video players.

5
Core concepts
🎯 02

Frame data

First frame ready.

Event
🔄 03

Not onload

Page vs media.

Compare
04

Hide spinner

UX pattern.

Pattern
05

+ onerror

Failed loads.

Pair

❓ Frequently Asked Questions

It runs JavaScript when the loadeddata event fires — when frame media data is available on audio or video.
<audio> and <video> — not body, img, or input.
onload is for pages and images. onloadeddata is a media-specific event on audio/video.
loadedmetadata = duration/size known. loadeddata = actual media frame data loaded.
addEventListener("loadeddata", handler) is preferred for production.
Yes in all modern browsers with HTML5 media; Internet Explorer 9+.

Master media load events

Practice the onloadeddata attribute with audio examples in the Try It editor.

Try onloadeddata 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