ondurationchange
ondurationchange event
The ondurationchange
event is activated when the duration of a media element (such as <audio>
or <video>
) is modified. This can happen when the media is loaded or when its metadata is refreshed.
Syntax
In HTML
<element ondurationchange="myScript">
In JS
object.ondurationchange = function(){myScript};
addEventListener()
function is used. In JavaScript, the
object.addEventListener("durationchange", myScript);
Example
<!DOCTYPE html>
<html>
<body>
<h1>HTML DOM Events</h1>
<h2>The ondurationchange Event</h2>
<video controls ondurationchange="myFunction()">
<source src="mov_bbb.mp4" type="video/mp4">
<source src="mov_bbb.ogg" type="video/ogg">
Your browser does not support video playback.
</video>
<p id="demo">Event has fired: </p>
<p>Video courtesy of <a href="https://institute.qarpeo.com" target="_blank">Tree</a>.</p>
<script>
function myFunction() {
document.getElementById("demo").innerHTML += "Duration has changed";
}
</script>
</body>
</html>
Value
Conclusion
The ondurationchange
event fires when the duration of a media element is updated. It’s commonly used with <audio>
and <video>
elements to respond to changes in media metadata. This event helps ensure the media is properly handled as its properties change.
oncanplaythrough
The oncanplaythrough event is triggered when the browser determines that a media file can be played from start to finish without interruption for buffering. This applies to both audio and video files.
onemptied
The onemptied event is triggered when an audio or video element is emptied. It does not occur when the media has finished loading, but only when the media source is removed or becomes unavailable.