I need to find out when an embedded youtube video ends via 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 I have so far to find out when it has ended but the alert doesnt come up. Could you help me find whats 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's new state: " + newState);
}
</script>
Thank.
You seem to have shown only part of the code. The object tag should contain an attribute called id that should be set to ytplayer. Here is the fully working code: