👀 Live Preview
Load the audio — when duration becomes known, status updates:
Duration: unknown (NaN until metadata loads)…

The ondurationchange attribute is an inline event handler that runs JavaScript when the durationchange event fires. That happens on <audio> and <video> whenever element.duration changes. Before metadata loads, duration is NaN; once the browser knows the total length, duration becomes a number in seconds and durationchange fires. Use it to update progress bars, show total runtime, or react when a live stream length changes. It does not fire on every playback tick—use ontimeupdate for current position.
Inline JS.
Media event.
Media elements.
NaN → seconds.
Preferred way.
Property API.
ondurationchange AttributeThe primary purpose of ondurationchange is to react when the browser learns or updates the total length of media. You might format and display runtime (“3:42”), enable a seek bar, or log when a stream’s duration shifts. The event is part of the media loading lifecycle alongside loadedmetadata, loadeddata, and canplay.
Read element.duration inside the handler—it is in seconds. Before metadata arrives, duration is NaN. The old reference used alert() in demos—avoid that in real apps. Status text or UI updates are clearer.
Production code should use video.addEventListener("durationchange", handler). Inline ondurationchange on <audio> or <video> works for learning but separates concerns less cleanly.
Set ondurationchange on audio or video, or assign element.ondurationchange:
<script>
function handleDurationChange(el) {
var secs = Math.floor(el.duration);
console.log("Duration: " + secs + " seconds");
}
</script>
<audio controls ondurationchange="handleDurationChange(this)">
<source src="song.mp3" type="audio/mpeg">
</audio>durationchange event fires.<audio> and <video> media elements.element.duration changes (including NaN → number).media.ondurationchange = function() { … }.media.addEventListener("durationchange", handler).The ondurationchange attribute accepts a string of JavaScript code:
ondurationchange="handleDurationChange(this)" — Pass the element to a function.ondurationchange="updateRuntime()" — Call a named function.video.ondurationchange = () => { … } assigns the handler.const audio = document.getElementById("track");
audio.addEventListener("durationchange", () => {
const secs = Math.floor(audio.duration);
document.getElementById("runtime").textContent = secs + " seconds";
});| Event | When it fires | Handler |
|---|---|---|
durationchange | element.duration changes | ondurationchange |
loadedmetadata | Metadata (incl. duration) ready | onloadedmetadata |
loadeddata | First frame ready | onloadeddata |
canplay | Enough data to start | oncanplay |
| Before metadata | duration is NaN | Wait for durationchange |
| Target | Supported? | Notes |
|---|---|---|
<audio> | Yes | Inline ondurationchange on audio |
<video> | Yes | Most common use |
<source> | No | Event fires on parent media element |
<input>, <img> | No | Not media duration events |
ondurationchange vs onloadedmetadata vs onloadeddata vs oncanplay| Handler | When it fires | Typical use |
|---|---|---|
onloadedmetadata | Metadata parsed (duration often known) | Read duration, dimensions, tracks |
ondurationchange | Whenever duration changes | Update total runtime UI |
onloadeddata | First frame of media available | Show poster / first frame |
oncanplay | Enough buffered to start | Enable play button, hide loader |
Inline ondurationchange on audio, dynamic property assignment, and durationchange vs loadedmetadata.
Load the audio — when duration becomes known, status updates:
Duration: unknown (NaN until metadata loads)…
Read element.duration when metadata arrives:
<audio controls ondurationchange="handleDurationChange(this)">
<source src="song.mp3" type="audio/mpeg">
</audio>
<script>
function handleDurationChange(el) {
var secs = Math.floor(el.duration);
console.log("Duration: " + secs + " seconds");
}
</script>The browser fires durationchange on the audio element when total length becomes known. Pass this to read duration inside the handler.
Assign element.ondurationchange after the element exists:
<script>
var video = document.getElementById("clip");
video.ondurationchange = function () {
var secs = Math.floor(video.duration);
console.log("Duration: " + secs + " seconds");
};
// Or addEventListener form:
video.addEventListener("durationchange", function () { /* … */ });
</script>Register the handler once at page load. When metadata arrives, durationchange runs and video.duration holds the total length in seconds.
See where durationchange fits and watch duration go from NaN to a known value:
console.log(audio.duration); // NaN before metadata
audio.addEventListener("loadedmetadata", () => { /* metadata ready */ });
audio.addEventListener("durationchange", () => {
console.log(Math.floor(audio.duration) + " seconds");
});durationchange tracks changes to element.duration, not playback position. For current time during playback, listen for timeupdate.
controls or fully accessible custom players with keyboard support.NaN.Browser fetches the file.
Total length not known yet.
Metadata parsed; seconds known.
Show total length.
The durationchange event and ondurationchange handler are supported in all modern browsers on <audio> and <video> elements — Chrome, Firefox, Safari, and Edge.
ondurationchange supportAll major browsers fire the underlying event and honor the ondurationchange handler attribute.
Bottom line: Universal support on media elements. Test with slow network throttling to see duration stay NaN longer before metadata arrives.
ondurationchange.durationchange fires.Math.floor in simple demos.audio/video for unsupported browsers.The ondurationchange attribute runs JavaScript when element.duration changes on audio or video. Use it to show total runtime, enable seek UI, or react when a stream length updates.
Prefer addEventListener("durationchange", …), handle NaN before metadata loads, and pair with onloadedmetadata or oncanplay when you need broader load-state signals.
ondurationchangeBookmark these before wiring your next event handler.
durationchange fires.
EventMedia only.
ScopeRead duration.
PatternPreferred.
ModernTotal length only.
Notedurationchange event fires — when element.duration changes on audio or video.audio and video elements. The event fires on the media element loading the resource.loadedmetadata fires when metadata is first available. durationchange fires whenever duration changes — including NaN to seconds or live stream updates.media.addEventListener("durationchange", …). Inline ondurationchange works for simple demos.Practice inline ondurationchange, dynamic assignment, and durationchange vs loadedmetadata in the Try It editor.
5 people found this page helpful