👀 Live Preview
Duration appears when metadata loads:
Duration: waiting for metadata…

The onloadedmetadata attribute is an inline event handler for the loadedmetadata event on media elements — <audio> and <video>. It runs when the browser has loaded metadata about the file: duration, and for video, width and height. That happens before loadeddata in the media timeline. Use it to show duration labels, size the player, or prepare the UI before frame data arrives. Pass the element to your handler (e.g. handleMetadata(this)) — this is not the media element inside a standalone function unless you bind it.
Inline JS.
Duration ready.
Media only.
Read length.
Preferred way.
Event order.
onloadedmetadata AttributeThe primary purpose of onloadedmetadata is to react as soon as the browser knows what the media file is — how long it is, how big the video frame is, and what tracks exist — even before enough data is buffered to paint the first frame. That is ideal for displaying “3:42” next to a progress bar or setting the video element’s aspect ratio.
In the loading sequence, loadstart → loadedmetadata → loadeddata → canplay. Metadata arrives early, so you can update labels while the rest of the file still downloads.
Use onloadedmetadata="showDuration(this)" or event.target in addEventListener. A bare function name does not get this as the video unless you assign it that way.
Set onloadedmetadata on an audio or video element:
<audio controls onloadedmetadata="showDuration(this)">
<source src="track.mp3" type="audio/mpeg">
</audio>
<video controls onloadedmetadata="handleMetadata(this)">
<source src="clip.mp4" type="video/mp4">
</video>loadedmetadata event fires.<audio> and <video> only.element.duration, videoWidth, videoHeight (video).this from inline handler or use event.target in listeners.media.addEventListener("loadedmetadata", handler).onerror if metadata cannot be loaded.The onloadedmetadata attribute accepts a string of JavaScript code:
onloadedmetadata="handleMetadata(this)" — Pass the media element.onloadedmetadata="showDuration(event.target)" — If using event in listener style.video.onloadedmetadata = () => { … } — inside, video.duration works.player.addEventListener("loadedmetadata", () => {
const mins = Math.floor(player.duration / 60);
const secs = Math.floor(player.duration % 60);
document.getElementById("duration").textContent =
mins + ":" + String(secs).padStart(2, "0");
});| Property / event | Available after loadedmetadata? | Notes |
|---|---|---|
duration | Yes | Seconds (may be NaN until loaded) |
videoWidth / videoHeight | Yes (video) | Intrinsic dimensions |
| First frame painted | Not yet | Use loadeddata |
| Playback ready | Maybe not | Use canplay |
| Handler attribute | onloadedmetadata | Inline on media element |
| Target | Supported? | Notes |
|---|---|---|
<audio> | Yes | Duration available |
<video> | Yes | Duration + dimensions |
<body> / window | No | Use onload |
<source> alone | No | Listen on parent media element |
onloadedmetadata vs onloadeddata vs oncanplay| Handler | When it fires | Typical use |
|---|---|---|
onloadedmetadata | Metadata parsed | Show duration, set aspect ratio |
onloadeddata | Frame data at current time | Hide spinner, show frame |
oncanplay | Can start playback | Enable play button |
Display duration on metadata load, addEventListener, and format mm:ss labels.
Duration appears when metadata loads:
Duration: waiting for metadata…
Read duration when metadata is ready:
<video controls onloadedmetadata="handleMetadataLoaded(this)">
<source src="example.mp4" type="video/mp4">
</video>
<p id="metaOut"></p>
<script>
function handleMetadataLoaded(media) {
document.getElementById("metaOut").textContent =
"Duration: " + media.duration.toFixed(1) + " s";
}
</script>Metadata loads before frame data. media.duration becomes available in the handler.
Attach with addEventListener:
<audio id="dynamicMedia" controls>
<source src="track.mp3" type="audio/mpeg">
</audio>
<script>
document.getElementById("dynamicMedia").addEventListener("loadedmetadata", function () {
document.getElementById("log").textContent =
"Metadata loaded! Duration: " + this.duration + " s";
});
</script>Register once in script. The callback runs when metadata is parsed — often before the user presses play.
Build a friendly time label from duration:
function formatDuration(seconds) {
const m = Math.floor(seconds / 60);
const s = Math.floor(seconds % 60);
return m + ":" + String(s).padStart(2, "0");
}
audio.addEventListener("loadedmetadata", () => {
label.textContent = formatDuration(audio.duration);
});loadedmetadata is the earliest reliable point to read duration for display.
canplay for play readiness.<track kind="captions"> for accessible video.Browser requests file.
Duration, size known.
onloadedmetadata runs.
UI adapts to media.
The loadedmetadata event and onloadedmetadata handler are supported wherever HTML5 audio and video are supported.
onloadedmetadata supportAll major browsers fire loadedmetadata on audio and video elements.
Bottom line: Reliable for duration and dimension labels on audio/video in all modern browsers.
this or use event.target to read durationaddEventListener("loadedmetadata", …) in productionisFinite(media.duration) before displayingonloadeddata and onerror for full UXthis is the video in a standalone function without passing itonloadedmetadata with body onload<source> — use parent media elementcanplay if neededThe onloadedmetadata attribute runs JavaScript when audio or video metadata is ready — the right moment to show duration, dimensions, and prepare the player UI.
Pass the media element explicitly, prefer addEventListener, and follow with onloadeddata when you need frame data or spinners hidden.
onloadedmetadataBookmark these before building media players.
Before frames.
EventLength in sec.
APIElement ref.
GotchaWidth/height.
VideoNext event.
Timelineloadedmetadata event fires — when duration and (for video) dimensions are known on audio or video.loadedmetadata = info about the file. loadeddata = frame data at the current time is loaded.onloadedmetadata="fn(this)" or event.target. A standalone function called without the element does not receive the video as this.duration is available on <audio> after metadata loads.addEventListener("loadedmetadata", handler) is preferred for production.Practice the onloadedmetadata attribute with duration display examples in the Try It editor.
5 people found this page helpful