javascript Youtube Iframe:onYouTubePlayerAPIReady() 未调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14303795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Youtube Iframe: onYouTubePlayerAPIReady() not called
提问by jenjis
I have a page with an iframe which load a youtube video (the src of iframe is modified in runtime). I based on code by Rob W provided in different answers on this topic
我有一个带有 iframe 的页面,它加载 youtube 视频(iframe 的 src 在运行时被修改)。我基于 Rob W 在这个主题的不同答案中提供的代码
<iframe id="browser" class="browser" scrolling="no" name="navigation"
src="http://www.youtube.com/embed/nOEw9iiopwI?enablejsapi=1" application="youtube" style="display:
inline;"></iframe>
Then, when iframe is loaded this code is executed:
然后,当 iframe 加载时,执行此代码:
$('.browser').load(function() {
dispose_ytplayer();
});
the called function dispose_ytplayer() is:
被调用的函数 dispose_ytplayer() 是:
function dispose_ytplayer() {
(function(){
var s = document.createElement("script");
s.src = "http://www.youtube.com/player_api";
var before = document.getElementsByTagName("script")[0];
before.parentNode.insertBefore(s, before);
})();
alert('called yt_dispose');
var ytIframeplayer;
function onYouTubePlayerAPIReady() {
alert('called onYouTubePlayerAPIReady');
ytIframeplayer = new YT.Player('browser', {
events: {
"onStateChange": stopCycle
}
});
}
}
but the second alert ("called onYouTubePlayerAPIReady") is never called, and my chrome console.log shows this error message from www-embed_core_module-vflNmuGQq.js:26:
但第二个警报(“onYouTubePlayerAPIReady”)从未被调用,我的 chrome console.log 显示了来自www-embed_core_module-vflNmuGQq.js:26 的错误消息:
Unsafe JavaScript attempt to access frame with URL http://mysite.comfrom frame with URL http://www.youtube.com/embed/nOEw9iiopwI?enablejsapi=1. Domains, protocols and ports must match.
不安全的 JavaScript 尝试从 URL http://www.youtube.com/embed/nOEw9iiopwI?enablejsapi=1 的框架访问 URL http://mysite.com的框架。域、协议和端口必须匹配。
Any ideas?
有任何想法吗?
回答by CD..
onYouTubePlayerAPIReady
should be on the window
object.
onYouTubePlayerAPIReady
应该在window
对象上。
try:
尝试:
window.onYouTubePlayerAPIReady = function() {
alert('called onYouTubePlayerAPIReady');
ytIframeplayer = new YT.Player('browser', {
events: {
"onStateChange": stopCycle
}
});
}
回答by SeinopSys
It seems like you're not closing the functions off correctly.
您似乎没有正确关闭这些功能。
The last }
is closing off onYouTubePlayerAPIReady()
, not dispose_ytplayer()
.
最后}
是关门onYouTubePlayerAPIReady()
,不是dispose_ytplayer()
。
Fixed code:
固定代码:
function dispose_ytplayer() {
(function(){
var s = document.createElement("script");
s.src = "http://www.youtube.com/player_api";
var before = document.getElementsByTagName("script")[0];
before.parentNode.insertBefore(s, before);
})();
alert('called yt_dispose');
var ytIframeplayer;
function onYouTubePlayerAPIReady() {
alert('called onYouTubePlayerAPIReady');
ytIframeplayer = new YT.Player('browser', {
events: {
"onStateChange": stopCycle
}
});
}
}