I’m trying to capture the YT object. The onYouTubePlayerReady() functions is called but I can’t seem to do anything with the object.
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("swfobject", "2.2");
</script>
<script type="text/javascript">
function onYouTubePlayerReady (playerID)
{
ytplayer = document.getElementById(playerID);
}
function play ()
{
if (ytplayer)
{
ytplayer.playVideo();
}
}
play();
</script>
ytplayer is referenced to the DOM and not the video itself (play therefor throws Uncaught TypeError: Object # has no method ‘playVideo’, but this is basically the example given by google.
What am I doing wrong?
I don’t see where you’re initializing your video player at all in your above code. The code below is working fine in Firefox. Substitute the video ID of the video you would like to play in place of [YOUR_VIDEO_ID] below and you should be all set. Can also confirm that the code below works like a champ in IE and Chrome as well.
I think what you’re missing is that you need to reference the “playerId” by whatever you passed in to the object initialization attributes (var atts = …) line. Doing a little bit of testing does reveal that the “onYouTubePlayerReady” function does not actual pass a valid id value that can be used to perform the DOM lookup.