Answer the question
In order to leave comments, you need to log in
Why is youtube player not working?
Hello!
On the listing, each product card has a video launch button, when clicked, a bootstrap popup (modal) opens in which the video immediately starts.
The strange thing is that on some elements the video starts and on some it doesn't.
An error is displayed in the browser console:
Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('https://www.youtube.com') does not match the recipient window's origin ('http://site.ru').
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 stopVideo(id_iframe) {
document.getElementById('vid_'+id_iframe).contentWindow.postMessage('{"event":"command","func":"stopVideo","args":""}', '*')
}
function init_video(video_id){
if (player) {
player.destroy();
}
console.log('init start!');
if(document.getElementById('vid_'+video_id).contentWindow){
document.getElementById('vid_'+video_id).contentWindow.postMessage('{"event":"command","func":"playVideo","args":""}', '*');
console.log('postMessage!');
}
var player;
player = new YT.Player('vid_'+video_id, {
height: '700',
videoId: video_id,
playerVars: {
rel: 0,
showinfo: 0
},
events: {
'onReady': onPlayerReady,
}
});
if(player){
console.log(player);
}
function onPlayerReady(event) {
console.log('onPlayerReady');
event.target.playVideo();
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question