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.
Syntax
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).
Values
Attribute Values
The attribute value is always a script. Common patterns:
Value / pattern
Description
Try it
script
Any JavaScript run when duration / dimensions / tracks are known
loadstart — the browser begins looking for media data.
loadedmetadata — duration / dimensions / tracks are known.
loadeddata — data for the current playback position is available.
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.
Compatibility
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 ChromeAll modern
Full support
Mozilla FirefoxAll modern
Full support
Apple SafariAll modern
Full support
Microsoft EdgeAll modern
Full support
OperaAll modern
Full support
Samsung InternetModern
Full support
Internet Explorer9+
Supported
onloadedmetadataWidely supported
Bottom line: Safe to use for reading duration and video size. Prefer addEventListener('loadedmetadata') in apps.
Remember
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.