CKTIS / HTML
ondurationchange — handling media duration changes
The ondurationchange attribute contains JavaScript executed for the durationchange event. It typically updates duration information for audio and video elements; it does not report ongoing playback progress.
Version: WHATWG HTML Living Standard; index snapshot date recorded separately
Syntax
ondurationchange="JavaScript statements"ondurationchange is an event handler content attribute for durationchange, typically used with audio and video elements. Its value is JavaScript code, not a Boolean or a duration value. Inside the attribute handler, this refers to the element and event provides the event object.
The browser fires this event when the established resource duration changes, for example when it becomes known or is revised. Read this.duration: the result is in seconds, NaN means no data is available, and Infinity indicates an unbounded duration. This is not a playback-progress event; use timeupdate for that purpose.
In JavaScript, you can instead assign a function to media.ondurationchange or use media.addEventListener('durationchange', handler). The event name has no on prefix. Property assignment replaces the existing handler in that slot; addEventListener supports independent listeners. A CSP policy can block code written directly in the attribute.
The example dispatches a synthetic event without fetching media. It checks the attribute-to-handler connection, not detection of an actual duration change. It does not change duration, so the expected text is durationchange: NaN.
Examples
<!doctype html>
<meta charset="utf-8">
<title>ondurationchange</title>
<audio id="media" ondurationchange="document.getElementById('result').textContent = event.type + ': ' + String(this.duration)"></audio>
<output id="result"></output>
<script>
const media = document.getElementById('media');
media.dispatchEvent(new Event('durationchange'));
</script>Result
durationchange: NaN
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.