Skip to content
the poor coder

iframe player youtube onfinish event

Poor Man
Published
Reading time
3 min read
On this page

How to Use the onfinish Event with an iFrame YouTube Player#

If you want to add a YouTube video to your website, you can use an iFrame player. The player provides an easy way to embed videos on your website without worrying about the technical details. However, if you want to customize the player's behavior, you need to use JavaScript. In this article, we will show you how to use the onfinish event with an iFrame YouTube player.

Step 1: Add the iFrame Player to Your Website#

The first step is to add the iFrame player to your website. You can get the code from the YouTube website. Here is an example:

Text
<div id="player"></div>

<script>
  var tag = document.createElement('script');
  tag.src = "https://www.youtube.com/iframe_api";
  var firstScriptTag = document.getElementsByTagName('script')[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

  var player;
  function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
      height: '360',
      width: '640',
      videoId: 'VIDEO_ID'
    });
  }
</script>

You need to replace "VIDEO_ID" with the ID of the YouTube video you want to play.

Step 2: Define the onfinish Event#

The next step is to define the onfinish event. This event is triggered when the video ends. Here is an example:

Text
<div id="player"></div>

<script>
  var tag = document.createElement('script');
  tag.src = "https://www.youtube.com/iframe_api";
  var firstScriptTag = document.getElementsByTagName('script')[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

  var player;
  function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
      height: '360',
      width: '640',
      videoId: 'VIDEO_ID',
      events: {
        'onStateChange': onPlayerStateChange
      }
    });
  }

  function onPlayerStateChange(event) {
    if (event.data == YT.PlayerState.ENDED) {
      // Your code goes here
    }
  }
</script>

In this example, we added the "events" property to the player object and defined the "onStateChange" event. We also defined the "onPlayerStateChange" function, which is called when the state of the player changes. If the state is "ENDED," we can execute our code.

Step 3: Add Your Code#

Finally, you can add your code to the "onPlayerStateChange" function. Here is an example:

Text
<div id="player"></div>

<script>
  var tag = document.createElement('script');
  tag.src = "https://www.youtube.com/iframe_api";
  var firstScriptTag = document.getElementsByTagName('script')[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

  var player;
  function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
      height: '360',
      width: '640',
      videoId: 'VIDEO_ID',
      events: {
        'onStateChange': onPlayerStateChange
      }
    });
  }

  function onPlayerStateChange(event) {
    if (event.data == YT.PlayerState.ENDED) {
      // Your code goes here
      console.log('Video ended');
    }
  }
</script>

In this example, we added a simple console.log statement to the "onPlayerStateChange" function. You can replace this with your own code. For example, you can display a message, hide the player, or redirect the user to another page.

Alternative Method: Use the YouTube Iframe API#

If you want more control over the player, you can use the YouTube iFrame API. This API allows you to interact with the player through JavaScript. Here is an example:

Text
<div id="player"></div>

<script>
  function onYouTubeIframeAPIReady() {
    var player = new YT.Player('player', {
      height: '360',
      width: '640',
      videoId: 'VIDEO_ID'
    });

    player.addEventListener('onStateChange', function(event) {
      if (event.data == YT.PlayerState.ENDED) {
        // Your code goes here
        console.log('Video ended');
      }
    });
  }
</script>

<script src="https://www.youtube.com/iframe_api"></script>

In this example, we used the "addEventListener" method to listen for the "onStateChange" event. When the event is triggered and the state is "ENDED," we can execute our code.

Conclusion#

Using the onfinish event with an iFrame YouTube player is easy. You just need to add the event listener and define your code. If you want more control over the player, you can use the YouTube iFrame API. With this API, you can interact with the player through JavaScript and customize its behavior.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC