Nick Gilbert Nick Gilbert - 1 year ago 148
Javascript Question

YouTube API onPlayerReady is Never Getting Called

Follow this tutorial, I have been trying to integrate the YouTube API into my website. Specifically, when a button is pressed I want to play a video hosted on YouTube in fullscreen mode. Unfortunately, I can't even get the code given in that tutorial to work.

I'm using Slim for markup so I added an empty div with the ID of player as the tutorial suggests


I then added this javascript at the end of my markup

function() {

Which should start the chain of calling these functions

function loadYoutubeAPI() {
var tag = document.createElement('script');
tag.src = "";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange

function onPlayerReady(event) {;

var done = false;
function onPlayerStateChange(event) {
if ( == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 6000);
done = true;

function stopVideo() {

However, the chain of calls stops before onPlayerReady fires and I can't figure out why.

Answer Source

From what I can see, it seems as though your issue may be in the player div seems to be of the class player and from what I can see in the tutorial, its looking for an div with player id. so something like the following may work:


I am not 100% familiar with Slim so you may need to mark this up differently.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download