The function to call youtube api at the end of the video

I need to know when the embedded YouTube video ends through javascript.

This is my embed code:

<object id="ytplayer" height="35" width="560"><param name="movie" value="http://www.youtube.com/v/u8C-ZTQJIkU?version=3&enablejsapi=1&playerapiid=ytplayer&rel=0&fs=0&theme=light&showinfo=0&modestbranding=1&autohide=0&color=white"> </param> <param name="allowFullScreen" value="false"> </param> <param name="allowscriptaccess" value="always"> </param> <embed src="http://www.youtube.com/v/u8C-ZTQJIkU?version=3&rel=0&fs=0&theme=light&showinfo=0&modestbranding=1&autohide=0&color=white" type="application/x-shockwave-flash" allowfullscreen="false" width="560" height="35" allowscriptaccess="always"></embed></object> 

This is the code that I still found out when it ended, but the warning does not appear. Could you help me find what is wrong?

  <script type="text/javascript" src="http://www.youtube.com/player_api"></script> <script type="text/javascript"> function onYouTubePlayerReady(playerId) { ytplayer = document.getElementById("ytplayer"); ytplayer.addEventListener("onStateChange", "onytplayerStateChange"); } function onytplayerStateChange(newState) { alert("Player new state: " + newState); } </script> 

Thanks.

+4
source share
1 answer

It seems you showed only part of the code. The object tag must contain the id attribute, which must be set in ytplayer . Here is the full working code:

  <object type="application/x-shockwave-flash" id="ytplayer" data="http://www.youtube.com/v/u8C-ZTQJIkU?enablejsapi=1&amp;playerapiid=ytplayer&amp;version=3" width="425" height="356"><param name="allowScriptAccess" value="always"> <param name="allowScriptAccess" value="always"> </object> </body> </html> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2.1/swfobject.js"></script> <script type="text/javascript"> function onYouTubePlayerReady(playerId) { var player = document.getElementById("ytplayer"); player.addEventListener("onStateChange", "onytplayerStateChange"); } function onytplayerStateChange(newState) { alert("New state " + newState); } </script> 
+3
source

Source: https://habr.com/ru/post/1412343/


All Articles