Tôi có video trên YouTube embedded on my site using an iFrame. Tôi muốn sử dụng the API để tạm dừng video, nhưng trong Chrome tôi nhận được một lỗi:Cách sử dụng API iFrame của YouTube trong Chrome và Firefox?
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.
Trong Firefox (3.6) tôi nhận được lỗi này:
Permission denied for <http://www.youtube.com> to call method Location.toString on <http://subdomain.example.com>.
Tôi đã thử đặt iFrame trong bản thân mình hoặc thêm nó với API. Dù bằng cách nào người chơi vào được, nhưng tôi không có quyền kiểm soát API.
Dưới đây là phần có liên quan của mã của tôi:
<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>
Làm thế nào để trình duyệt cho phép iFrame YouTube truy cập khung chính trang của tôi?
Cảm ơn!
Tôi vẫn gặp lỗi khi sử dụng API trình phát JavaScript để nhúng iframe. Tôi thậm chí đã thử mã của họ trong sân chơi của họ với trình duyệt của họ. – Josh