How to Detect the End of an HTML Video with JavaScript?

Estimated read time 1 min read

You can detect the end of an HTML video using JavaScript by listening to the ended event of the HTML video element.

Here’s an example of how to detect the end of a video and display an alert message:

<!DOCTYPE html>
<html>
  <head>
    <title>My Website</title>
  </head>
  <body>
    <video id="myVideo" src="yourvideo.mp4"></video>
    <script>
      let video = document.getElementById("myVideo");
      video.addEventListener("ended", function() {
        alert("The video has ended");
      });
    </script>
  </body>
</html>

In this example, we first use JavaScript to select the video element with an id of “myVideo”. We then attach an event listener to the video element that listens for the ended event. When the ended event fires, the function inside the event listener is called, which displays an alert message.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply