如何在Chrome和Firefox中使用YouTube iFrame API?

Ada*_*dam 10 youtube iframe firefox google-chrome youtube-api

使用iFrame在我的网站嵌入了YouTube视频.我想使用API暂停视频,但在Chrome中我收到错误消息:

Unsafe JavaScript attempt to access frame with URL http://subdomain.example.com/ from frame with URL http://www.youtube.com/embed/KmtzQCSh6xk?enablejsapi=1&origin=http://subdomain.example.com. Domains, protocols and ports must match.
Run Code Online (Sandbox Code Playgroud)

在Firefox(3.6)中我收到此错误:

Permission denied for <http://www.youtube.com> to call method Location.toString on <http://subdomain.example.com>.
Run Code Online (Sandbox Code Playgroud)

我已尝试将iFrame放入自己或使用API​​添加它.无论哪种方式玩家进入,但我没有API控制它.

这是我的代码的相关部分:

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

<script type="text/javascript">
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

var player;
function onYouTubePlayerAPIReady() {
    player = new YT.Player('player', {
        height: '433',
        width: '731',
        videoId: 'KmtzQCSh6xk'
      });
}

$("#pause_button").click(function(){
  alert("Pausing " + player);//is undefined :(
  player.pauseVideo();
});

</script>
Run Code Online (Sandbox Code Playgroud)

如何让浏览器允许YouTube iFrame访问我网页的主框架?

谢谢!

a p*_*erd 6

  1. 忽略这些警告.无论如何,玩家仍然可以正常工作.

  2. 使用最近推出的iframe嵌入式JavaScript Player API.