When using the Youtube iframe API, onPlayerReady is never called, and the players are only partially loaded.
I have tried some things from other posts:
Here two solutions were proposed:
Setting the
origin
variable to match the server domain: I tried to set it both in the iframesrc
attribute in the HTML tag, and inplayerVars
when initializing the player. No changes seem to have happened.Replacing
<iframe>
for<div>
tags: This did work in other projects, but not here. Instead I get an error I never had before, when javascript tries to create the player:Uncaught TypeError: tubeId.indexOf is not a function at returnFn (mytube.js:3115) at Xg.c [as R] (www-embed-player.js:519) at gh (www-embed-player.js:513) at dh (www-embed-player.js:512) at Xg.a.w (www-embed-player.js:509) at $g (www-embed-player.js:510) at Xg.h.da (www-embed-player.js:505) at www-embed-player.js:533
The site has two players, and although I only replaced the tags in one of them, both players started throwing this error. Also, the player variables returned undefined
now.
This post says the solution is setting enablejsapi
to 1, but I already did so in the HTML iframe tag.
These errors are not happening in all computers. All of them were detected on a Windows 8 machine running Google Chrome (couldn't test on other browsers). The same happened in google Chrome on MacOS (although I couldn't test the last changes which triggered the mytube.js error there). In my computer (windows 7, tested the site on Chrome and Firefox) everything works mostly well, except for an occasional "Failed to execute 'postMessage' on 'DOMWindow'" error (it says the player origin is set to http://www.youtube.com, although I already replaced it). This error doesn't pop up always, but maybe 30% of the times.
This is how I create the players:
HTML:
<iframe id="video1" type="text/html" src="https://www.youtube.com/embed/?rel=0&enablejsapi=1&origin=https://mywebsite.com"></iframe>
<!-- Video ID is loaded on user interaction, using loadVideoById() -->
JS/jQuery:
$(document).ready(function() {
loadYouTubeAPI();
});
function loadYouTubeAPI(){
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
}
function onYouTubeIframeAPIReady() {
player = new YT.Player('video1', {
playerVars: {'origin':'https://mywebsite.com/'},
events: {
'onReady': onPlayerReady1
},
});
}
function onPlayerReady1(event) { //I have different players that do different things when ready; I need different names for each onPlayerReady function
(...)
}
Here are screenshots of how the console returns the player variables: left is how the players are being created now, right is how they should be. In the left image the player is missing all of the methods, so I cannot control it.