HTML ondurationchange Attribute

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

Definition and Usage

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.

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).

Attribute Values

The attribute value is always a script. Common patterns:

Value / patternDescriptionTry it
scriptAny JavaScript run when duration is updatedTry it
ondurationchange="fn()"Call a named function from the attributeTry it
addEventListener("durationchange")Preferred modern listener on the media elementTry it
vs loadedmetadataCompare when duration updates vs when metadata is knownTry it

Live Preview

Load a sample clip. When duration updates, the status shows the new length. Use Reload to fire durationchange again.

Media

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

Player

Waiting for durationchange…

HTML Examples

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

Example 1 — Show Duration on Update

Read duration whenever it changes.

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

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

How It Works

  1. Put ondurationchange on the media element — not on <source>.
  2. Browser learns the length — duration updates from NaN to seconds.
  3. Handler runs — read clip.duration.
  4. Guard with isFinite — live streams may report Infinity.

Example 2 — durationchange After Changing Source

Call load() after changing src — duration updates again.

HTML
<video id="clip" width="400" height="225" controls>
  <source id="src" src="/video/count.mp4" type="video/mp4">
</video>
<button type="button" id="reload">Reload media</button>
<p id="log">Waiting…</p>

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

  clip.addEventListener("durationchange", function () {
    count += 1;
    var d = clip.duration;
    log.textContent =
      "durationchange #" + count + ": " +
      (isFinite(d) ? d.toFixed(2) + "s" : String(d));
  });

  document.getElementById("reload").addEventListener("click", function () {
    src.src = "/video/count.mp4";
    clip.load();
  });
</script>
Try It Yourself

How It Works

  1. First load — durationchange fires when length becomes known.
  2. Reload / new source — call media.load() after updating src.
  3. Duration updates again — the same handler can run more than once.
  4. Count events — useful when building playlists or source switchers.

Example 3 — durationchange vs loadedmetadata

Log both events so you can see how they relate on first load.

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

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

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

  clip.addEventListener("loadedmetadata", function () {
    add("loadedmetadata — metadata known");
  });

  clip.addEventListener("durationchange", function () {
    var d = clip.duration;
    add(
      "durationchange — duration = " +
      (isFinite(d) ? d.toFixed(2) + "s" : String(d))
    );
  });
</script>
Try It Yourself

How It Works

  1. loadedmetadata — duration, dimensions, and tracks are known.
  2. durationchange — specifically means duration was updated.
  3. Often close together on first load — order can vary slightly by browser.
  4. 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.

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 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
ondurationchange Widely supported

Bottom line: Safe to use for tracking media length. Prefer addEventListener('durationchange') in apps, and guard Infinity for live media.

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.

Next: onemptied

Learn when a media element’s network state becomes empty after a load reset.

onemptied 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