In JavaScript, using the addEventListener()
method:
object.addEventListener("ended",
myScript);
This example uses the addEventListener()
method to attach an "ended" event to an audio element.
Press play and wait for the audio to end.
<!DOCTYPE html> <html> <body> <audio id="myAudio" controls> <source src="sound.ogg" type="audio/ogg"> <source src="sound.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio>// ww w.ja va 2 s.c o m <p id="demo"></p> <script> document.getElementById("myAudio").addEventListener("ended", myFunction); function myFunction() { document.getElementById("demo").innerHTML = "The audio has ended."; } </script> </body> </html>
Bubbles: | No |
---|---|
Cancelable: | No |
Event type: | Event |
Supported HTML tags: | <audio> and <video> |