【问题标题】:How to use the YouTube iFrame API in Chrome and Firefox?如何在 Chrome 和 Firefox 中使用 YouTube iFrame API?
【发布时间】:2011-06-24 13:10:46
【问题描述】:

我有一个 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 访问我页面的主框架?

谢谢!

【问题讨论】:

    标签: firefox iframe google-chrome youtube youtube-api


    【解决方案1】:
    1. 忽略这些警告。无论如何,播放器应该仍然可以正常工作。

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

    【讨论】:

    • 我在使用 JavaScript Player API 进行 iframe 嵌入时仍然遇到错误。我什至用他们的浏览器在操场上尝试了他们的代码。
    猜你喜欢
    • 2012-12-18
    • 2015-12-25
    • 2014-09-22
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    相关资源
    最近更新 更多