I have implemented iframe_api for youtube clips on my site with following code.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// Setup Youtube player
var player;
// Get id of first clip
var ytVideoId = jQuery('.start-yt').attr('data-video-id');
// When API is ready, create player with above id
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: ytVideoId,
events: {
'onReady': onPlayerReady
}
});
console.log('API Ready');
}
// When player is ready
function onPlayerReady(event) {
jQuery('.start-yt').addClass('play-icon');
// Set glob var
var ytGlobId ="";
// Close button
jQuery('.ytClose').click(function(){
jQuery('.youtube-player-container').removeClass('active');
player.pauseVideo();
});
jQuery('.youtube-player-container').click(function(){
jQuery('.youtube-player-container').removeClass('active');
player.pauseVideo();
});
// load or play clip
jQuery('.start-yt').click(function(){
jQuery('.youtube-player-container').addClass('active');
if (ytGlobId == jQuery(this).attr('data-video-id')) {
player.playVideo();
} else {
ytGlobId = jQuery(this).attr('data-video-id');
player.loadVideoById(ytGlobId);
}
});
console.log('Player Ready');
}
This works. But a friend have Windows 10 and IE11 + Edge. And it seems that 'onReady': onPlayerReady
do not fire there. console.log
do write API Ready
but not Player Ready
when we have checked. It gives no error in the console, it just do not fire and works. I have checked in Browserstack with the same combinations (Windows 10 + IE11/Edge) and there it seems to work.
I have searched google for answers, and it seems some people have had similar problems, but I have not found an example exact enough to find a solution.