我有一个 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 访问我页面的主框架?
谢谢!
最佳答案
忽略这些警告。不管怎样,播放器应该仍然可以正常工作。
使用最近推出的 JavaScript Player API for iframe embeds .
关于firefox - 如何在 Chrome 和 Firefox 中使用 YouTube iFrame API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4952282/