The HTML onloadeddata event
The HTML onloadeddata attribute runs JavaScript when the first frame of the media has loaded. It is an inline handler for the loadeddata event; in modern code prefer addEventListener('loadeddata', …).
Overview
The onloadeddata event attribute runs JavaScript when the first media frame is available. In JavaScript the event itself is named loadeddata — drop the on prefix when you call addEventListener.
It is one of the media events, fired by <audio> and <video> elements as their loading and playback state changes. These events drive custom players — progress bars, buffering spinners, play/pause UI.
You can wire this up with the inline onloadeddata HTML attribute, but the modern, recommended approach is element.addEventListener('loadeddata', handler) in JavaScript. That keeps behavior out of your markup, lets you attach several handlers to the same event, and makes them easy to remove. The inline attribute is fine for quick demos.
Syntax
<element onloadeddata="handler()">…</element>
element.addEventListener('loadeddata', handler);
Best practices
- Prefer
element.addEventListener('loadeddata', handler)over the inlineonloadeddataattribute — it separates behavior from markup and allows multiple handlers. - Attach media events to the <video>/<audio> element to build custom player UI.
- Read the element's state (
currentTime,duration,buffered) inside the handler. - Still provide native
controlsand captions for accessibility.
Frequently asked questions
What is the onloadeddata event?
loadeddata.Which elements fire this event?
How do I build a custom video player?
currentTime and paused on the element to drive your own controls.Should I use the onloadeddata attribute or addEventListener?
addEventListener('loadeddata', …) in JavaScript. The inline onloadeddata attribute works but mixes behavior into the markup and allows only one handler per element.