Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onYouTubeIframeAPIReady() not firing

Tags:

I've looked through so many questions and the youtube api stuff but for the life of me can't figure out why the onYouTubeIframeAPIReady is not working.

Here is my iframe:

<iframe id="youtube_vid" width="763" height="430" src="https://www.youtube.com/embed/dlJshzOv2cw?theme=light&amp;showinfo=0&amp;rel=0&amp;enablejsapi=1" frameborder="0" allowfullscreen=""></iframe> 

And my script:

function callYTapi() {      var tag = document.createElement('script');     tag.src = "https://www.youtube.com/iframe_api";     var firstScriptTag = document.getElementsByTagName('script')[0];     firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);     console.log('script loaded');      function onYouTubeIframeAPIReady() {         console.log('IframeAPI = Ready');         var player = new YT.Player('youtube_vid', {             events: {                 'onReady': onPlayerReady,                 'onStateChange': onPlayerStateChange             }         });     }      function onPlayerReady(event) {         event.target.playVideo();     }      function onPlayerStateChange(event) {         if (event.data == YT.PlayerState.PAUSED) {             console.log("Paused");         }          if (event.data == YT.PlayerState.PLAYING) {             console.log("Playing");         }          if (event.data == YT.PlayerState.ENDED) {             end();          }     } } 

I get the console.log for the loaded script but not for Iframe ready or anything else. Any ideas? Thanks

like image 525
JGVM Avatar asked Apr 10 '14 16:04

JGVM


1 Answers

The callback functions must be in the global scope. Just move onYouTubeIframeAPIReady and the others outside callYTapi.

like image 133
Vinicius Pinto Avatar answered Sep 21 '22 05:09

Vinicius Pinto