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

前端 未结 5 2221
渐次进展
渐次进展 2020-12-16 22:36

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 thi

相关标签:
5条回答
  • 2020-12-16 22:51

    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.

    0 讨论(0)
  • 2020-12-16 22:55

    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.

    0 讨论(0)
  • 2020-12-16 23:06

    In my Case "widget_referrer" was missing. If nothing works for you, try widget_referrer :window.location.href

    other possible properties one might miss are

    origin:window.location.href, enablejsapi:1,

    0 讨论(0)
  • 2020-12-16 23:13

    In my case (similar to the selected answer here Youtube API error - Failed to execute 'postMessage' on 'DOMWindow':) It came down the to the fact that the video was not visible on the page at the time Player was instantiated.

    0 讨论(0)
  • 2020-12-16 23:15

    My website is configured without the www and the youtube link was with www that's why i was getting this error. Try to make it similar, I removed www from my youtube, and it works.

    0 讨论(0)
提交回复
热议问题