Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

onloadedmetadata — handling media metadata loading

The onloadedmetadata attribute contains JavaScript that handles the loadedmetadata event, usually on audio or video. It signals metadata availability, not completion of the download or readiness for uninterrupted playback.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

onloadedmetadata="JavaScript statements"

onloadedmetadata is an HTML event-handler attribute for loadedmetadata. Its value is JavaScript code, not an event name or a Boolean value. It is allowed on HTML elements, but its typical use concerns audio and video.

The browser fires loadedmetadata when metadata first becomes available for the resource being loaded: readyState reaches at least HAVE_METADATA. You can then read duration and, for video, videoWidth and videoHeight. Duration can be Infinity when the resource has no known end. This does not mean the entire file has downloaded; loadeddata concerns availability of the current frame's data.

Inside the attribute code, event is the event and this is the element. In JavaScript, you can instead assign a function to media.onloadedmetadata or use media.addEventListener('loadedmetadata', handler). The event name has no on prefix. Assigning the property replaces its previous function; addEventListener allows independent handlers. A CSP policy can block code in attributes.

The example deliberately dispatches a synthetic event to test the attribute without a media file. It neither loads metadata nor changes readyState. For real media loading, register the handler before starting to fetch the resource.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>onloadedmetadata demo</title>
<audio id="media" onloadedmetadata="document.getElementById('result').textContent = event.type + ' handled'"></audio>
<p id="result">Waiting</p>
<script>
  const media = document.getElementById('media');
  media.dispatchEvent(new Event('loadedmetadata'));
</script>
</html>

Result

loadedmetadata handled

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources