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

标签 firefox iframe google-chrome youtube youtube-api

我有一个 YouTube 视频 embedded on my site using an iFrame 。我想使用the 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.

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

Permission denied for <http://www.youtube.com> to call method Location.toString on <http://subdomain.example.com>.

我尝试过将 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>

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

谢谢!

最佳答案

  1. 忽略这些警告。不管怎样,播放器应该仍然可以正常工作。

  2. 使用最近推出的 JavaScript Player API for iframe embeds .

关于firefox - 如何在 Chrome 和 Firefox 中使用 YouTube iFrame API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4952282/

相关文章:

php - AJAX/jQuery 和 Chrome 的奇怪行为

python - Firefox 本地存储外部访问

javascript - 无法检测到我的 IE 11 浏览器

javascript - 将插件窗口附加到 Firefox 浏览器窗口

javascript - 为什么 Firefox 在页面加载时触发 mouseenter 事件?

javascript - Firefox,查询选择器和可见的伪选择器

javascript - 从框架引用窗口

javascript - MTurk 上的 jQuery,为什么 Chrome 报告 "Unsafe JavaScript attempt to access frame with URL"?

html - 无法在 iframe 中隐藏滚动条?

php - 无法在 Chrome 或 Firefox 中下载生成的 .ics 文件