Failed to execute 'postMessage' on 'DOMWindow' API Youtube

2

The situation is as follows:

I have 2 YouTube videos that are hidden, when I click the link this opens the video.

Now trying to use the Youtube API, which I want to give me some data, I mark the following error:

Failed to execute 'postMessage' on 'DOMWindow'

It is worth mentioning that I am at the local level and I want to know what that problem is.

This is my code:

$("#primervideo").click(function(e){
            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() {
            var player;
            player = new YT.Player('yt_video', {
                events: {
                    'onStateChange': onStateChange
                }
            });
        } 

This I occupy when I click on the link that calls the iframe, obviously this iframe is hidden and activated by a link

<iframe  width="560" height="315" src="https://www.youtube.com/embed/RSo2gq8wTYw?autoplay=1&rel=0&showinfo=0&version=3&enablejsapi=1" frameborder="0" allowfullscreen></iframe>
    
asked by cignius 02.08.2016 в 17:47
source

1 answer

3

First, the URL you use is not valid. Second, at the end of the URL, find the video ID. Example:

  

link   ID = ANS9sSJA9Yc

Third, can you use a div instead of an iframe?

<div id="player"></div>

And from the JS:

var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";

var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

var player;
function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
        height: '390',
        width: '640',
        videoId: 'ANS9sSJA9Yc',
        events: {
            'onReady': function onPlayerReady(event) {
              player.setPlaybackRate(2);
              event.target.playVideo();
          }
        }
    });
}

DEMO: link

    
answered by 15.10.2016 / 17:50
source