HTML onloadedmetadata Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The onloadedmetadata attribute is an event-handler attribute for <audio> and <video>. It runs JavaScript when the loadedmetadata event fires — media metadata has loaded.

After this event, MDN notes that duration, dimensions (for video), and tracks are known. Frame data may still be loading — that is onloadeddata, then usually oncanplay.

Baseline · Widely available.

Not deprecated and not experimental. The loadedmetadata event is stable across modern browsers since about July 2015.

HTML Syntax

syntax.html
<video|audio onloadedmetadata=" script">

script is JavaScript that runs when loadedmetadata fires. Equivalent JS: media.onloadedmetadata = handler or media.addEventListener("loadedmetadata", handler).

Attribute Values

The attribute value is always a script. Common patterns:

Value / patternDescriptionTry it
scriptAny JavaScript run when duration / dimensions / tracks are knownTry it
onloadedmetadata="fn()"Call a named function from the attributeTry it
addEventListener("loadedmetadata")Preferred modern listener on the media elementTry it
With other media eventsUsually before loadeddata and canplayTry it

Live Preview

Load a sample clip. When metadata is ready, duration (and video size) appear. Use Reload to fire loadedmetadata again.

Media

Sample media from /video/count.mp4 or /audio/count.mp3.

Player

Waiting for loadedmetadata…

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Show Audio Duration

Read duration as soon as metadata is available.

HTML
<audio id="clip" controls onloadedmetadata="showDuration()">
  <source src="/audio/count.mp3" type="audio/mpeg">
  Your browser does not support the audio element.
</audio>
<p id="status">Waiting for metadata…</p>

<script>
  function showDuration() {
    var clip = document.getElementById("clip");
    var secs = clip.duration;
    document.getElementById("status").textContent =
      "Duration: " + (isFinite(secs) ? secs.toFixed(2) + "s" : "unknown");
  }
</script>
Try It Yourself

How It Works

  1. Put onloadedmetadata on the media element — not on <source>.
  2. Browser parses headers — duration (and tracks) become available.
  3. Read clip.duration — seconds as a number; use isFinite for live/unknown streams.
  4. Update the UI — show length before the user presses play.

Example 2 — Video Duration and Dimensions

Use addEventListener and read videoWidth / videoHeight.

HTML
<video id="clip" width="400" height="225" controls>
  <source src="/video/count.mp4" type="video/mp4">
  Your browser does not support the video element.
</video>
<p id="log">Loading metadata…</p>

<script>
  var clip = document.getElementById("clip");
  var log = document.getElementById("log");

  function showMeta() {
    var d = clip.duration;
    log.textContent =
      "Duration: " + (isFinite(d) ? d.toFixed(2) + "s" : "unknown") +
      " · Size: " + clip.videoWidth + "×" + clip.videoHeight;
  }

  clip.addEventListener("loadedmetadata", showMeta);
  if (clip.readyState >= 1) showMeta();
</script>
Try It Yourself

How It Works

  1. Listen for loadedmetadata on the <video>.
  2. Read duration for length in seconds.
  3. Read videoWidth / videoHeight — intrinsic media size (not the CSS/layout box).
  4. Check readyState >= 1 (HAVE_METADATA) if the listener might attach late.

Example 3 — Where loadedmetadata Fits in the Load Order

Log related media events so you can see the usual sequence.

HTML
<audio id="audio" controls>
  <source src="/audio/count.mp3" type="audio/mpeg">
</audio>
<ul id="log"></ul>

<script>
  var audio = document.getElementById("audio");
  var log = document.getElementById("log");

  function add(line) {
    var li = document.createElement("li");
    li.textContent = line;
    log.appendChild(li);
  }

  audio.addEventListener("loadstart", function () {
    add("loadstart — looking for media");
  });
  audio.addEventListener("loadedmetadata", function () {
    add("loadedmetadata — duration known");
  });
  audio.addEventListener("loadeddata", function () {
    add("loadeddata — current frame data ready");
  });
</script>
Try It Yourself

How It Works

  1. loadstart — the browser begins looking for media data.
  2. loadedmetadata — duration / dimensions / tracks are known.
  3. loadeddata — data for the current playback position is available.
  4. Next steps often include canplay when playback can start.

Edge Cases & Pitfalls

Most surprises come from reading duration too early or confusing metadata with frame data.

timing

duration is NaN before metadata

Reading media.duration in a script that runs before loadedmetadata often yields NaN. Wait for the event (or check readyState >= 1).

live

Live streams may have infinite duration

Some streams report duration as Infinity. Guard with isFinite(duration) before formatting.

meaning

Metadata ≠ first frame

You know length and size, but the current frame may not be ready yet. Use onloadeddata for that.

target

Listen on audio/video, not source

The event targets the media element. Nested <source> tags do not receive onloadedmetadata.

preload

preload="none" delays metadata

With preload="none", the browser may wait until the user plays before fetching enough to know duration.

Browser Support

onloadedmetadata is an HTML event-handler attribute for media elements in the browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

onloadedmetadata

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support loadedmetadata on audio and video.

97% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet Modern
Full support
Internet Explorer 9+
Supported
onloadedmetadata Widely supported

Bottom line: Safe to use for reading duration and video size. Prefer addEventListener('loadedmetadata') in apps.

Key Takeaways

  • Role: metadata loaded — duration, dimensions, and tracks are known.
  • readyState: typically HAVE_METADATA (1) or greater.
  • Order: after loadstart; before loadeddata / canplay.
  • API: use duration, videoWidth, videoHeight — guard with isFinite.

One line: onloadedmetadata is when you can safely show length and video size — not when the first frame is painted.

Frequently Asked Questions

It is an event-handler attribute on audio or video. When media metadata has loaded, the loadedmetadata event fires and the JavaScript in onloadedmetadata runs. Duration, dimensions (for video), and track info are then available.
The HTML audio and video elements. Put the handler on the media element itself, not on nested source tags.
Typically media.duration, and for video also videoWidth and videoHeight. Track lists are also known. Media frame data may still be loading.
loadedmetadata means duration and dimensions (or track info) are known. loadeddata means media data for the current playback position has loaded — often the first frame you can display.
You read duration too early — before metadata loaded — or the stream is live/unknown length (duration can be Infinity). Wait for loadedmetadata, then check for a finite number.
No. The loadedmetadata event and onloadedmetadata handler are standard HTML media features and Baseline widely available since about July 2015.

Next: onloadstart

Learn when the browser begins fetching media data.

onloadstart tutorial →

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.

8 people found this page helpful