I have a youtube iframe video, and I want to close it when it ends.
This should be working, but it is not. The fancybox launches and the video displays correctly, but none of the console.log
appears. What am I doing wrong?
$.fancybox({
href: "https://www.youtube.com/embed/8CR-zM6jwD8?rel=0&autoplay=1",
type: "iframe",
beforeShow: function () {
// Find the iframe ID
var id = $.fancybox.inner.find('iframe').attr('id');
// Create video player object and add event listeners
var player = new YT.Player(id, {
events: {
'onStateChange': function (event) {
console.log('onStateChange fired');
if (event.data === 0) {
console.log('ended');
$.fancybox.close();
} // if
} // onStateChange
} // events
}); // YT.Player
loadPage();
}, // beforeShow
}); // fancybox