D
D
DeeUs2019-10-16 11:20:37
YouTube
DeeUs, 2019-10-16 11:20:37

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').

youtube player 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);
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();
  }
}


The site is powered by wordpress

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question