Sunday, December 24, 2017

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

Leave a Comment

I'm trying to start a youtube video when a modal is opened and not progress to the next page until it is completed.

My script below works in Chrome but produces this error in Firefox and Edge.

Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('https://www.youtube.com') does not match the recipient window's origin ('http://example.com').

Javascript

<script src="http://www.youtube.com/player_api"></script>  <script>      // autoplay video     function onPlayerReady(event) {         event.target.playVideo();     }      // when video ends     function onPlayerStateChange(event) {                 if(event.data === 0) {                       alert('Thank you for watching - Click OK to see your results');         }     }  </script>  <script language="JavaScript">     $(document).ready(function() {         $('#post_form').submit(function() { // catch the form's submit event             $("#adModal").modal("show");             var time = $('#adtime').val();             //startCountdownTimer(time) ;              // create youtube player             var player;             player = new YT.Player('player', {                 width: '640',                 height: '390',                 videoId: 'EF-jwIv1w68',                 host: 'http://www.youtube.com',                 events: {                     onReady: onPlayerReady,                     onStateChange: onPlayerStateChange                 }             });          });     }); </script> 

I have reviewed this question/answer but cannot seem to get it working with my code by amending the http / https.

2 Answers

Answers 1

I think that the error message is a little bit misleading, as it has nothing to do with your actual host, but is more about how resources from youtube.com are referenced on the page.

There are two things I would suggest in order to get rid of this error message. (At least these were working in my case.)

First and foremost you should reference the IFrame Player API script via https. When called with http, YouTube redirects that script request automatically to it’s https counterpart, so if you reference the script directly from https, that eliminates this extra redirect. But most importantly, in case your production environment ever goes to https, it won’t load that script over http, but will throw a “Blocked loading mixed active content” error.

According to my tests this change alone would already magically solve the issue. However in case you would prefer to leave that on http, there is this other thing:

Reading through the Loading a Video Player and the Supported Parameters sections of the API docs, there is no mention about the host parameter at all. In fact when I removed that line from the Player parameters I didn't receive the error message any more. Also, interestingly, when I set host literally to http://www.example.com, the error message reads: The target origin provided (‘http://www.example.com’) does not match the recipient window’s origin ….) Therefore I think the host parameter should not be set by the client.

Sidenote: If you have a look at the contents of https://www.youtube.com/player_api, you will see this statement: var YTConfig = {'host': 'http://www.youtube.com'};. To me it means that http://www.youtube.com is some kind of a default for host anyways, so even if you go and set it in the client code, you could try to set it to https://www.youtube.com.

Long story short, try to use <script src="https://www.youtube.com/player_api"></script> and comment out the host. This is my 2 cents.

Answers 2

The solution was simple, although I do not know why. By removing the api from the modal it worked fine. Within the modal it wouldn't.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment