The ondurationchange attribute is an event-handler attribute for <audio> and <video>. It runs JavaScript when the durationchange event fires — the media element’s duration property has been updated.
That often happens once when metadata loads (NaN → seconds). It can fire again if you change the source or a live stream’s length changes. Closely related: onloadedmetadata (metadata known) and ontimeupdate (playback position while playing).
Baseline · Widely available.
Not deprecated and not experimental. The durationchange event is stable across modern browsers since about July 2015.
Discouraged as inline HTML.
MDN discourages HTML event handler attributes. Prefer media.addEventListener("durationchange", handler) in production; use inline ondurationchange for quick demos and learning.
Syntax
HTML Syntax
syntax.html
<video|audio ondurationchange="script">
script is JavaScript that runs when duration updates. Equivalent JS: media.ondurationchange = handler or media.addEventListener("durationchange", handler).
Values
Attribute Values
The attribute value is always a script. Common patterns:
loadedmetadata — duration, dimensions, and tracks are known.
durationchange — specifically means duration was updated.
Often close together on first load — order can vary slightly by browser.
Use durationchange when you care about length updates over the whole lifetime of the element.
Edge Cases & Pitfalls
Most surprises come from reading duration too early or forgetting live streams.
timing
duration is NaN before it updates
Reading media.duration before the first durationchange / metadata load often yields NaN. Wait for the event.
live
Live streams may be Infinity
Unknown-length streams can report duration === Infinity. Guard with isFinite(duration) before showing a clock.
repeat
Can fire more than once
Changing src, calling load(), or stream updates can update duration again. Do not assume a single fire.
target
Listen on audio/video, not source
The event targets the media element. Nested <source> tags do not receive ondurationchange.
preload
preload="none" delays duration
With preload="none", the browser may wait until play before fetching enough to know length.
Compatibility
Browser Support
ondurationchange 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
ondurationchange
Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support durationchange 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
ondurationchangeWidely supported
Bottom line: Safe to use for tracking media length. Prefer addEventListener('durationchange') in apps, and guard Infinity for live media.
Remember
Key Takeaways
Role: fires when media.duration is updated.
First load: often NaN → seconds when metadata arrives.
vs metadata:loadedmetadata = metadata known; durationchange = duration value changed.
Guard: use isFinite(duration) for live / unknown length.
One line:ondurationchange is when media length updates — read duration, don’t assume it fires only once.
Frequently Asked Questions
It is an event-handler attribute on audio or video. When the media duration property is updated, the durationchange event fires and the JavaScript in ondurationchange runs.
The HTML audio and video elements. Put the handler on the media element itself, not on nested source tags.
Whenever duration is updated — commonly when metadata first loads (NaN → seconds), and again if you change the source or a stream’s length changes.
loadedmetadata means metadata (duration, dimensions, tracks) is known. durationchange specifically means the duration value changed. They often happen close together on first load, but durationchange can fire again later.
NaN means duration is not known yet. Infinity is common for live/unknown-length streams. Wait for durationchange (or loadedmetadata), then check isFinite(duration) before formatting.
No. The durationchange event and ondurationchange handler are standard HTML media features and Baseline widely available since about July 2015.