javascript / youtube api - 未定义变量 YT
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11003216/
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
javascript / youtube api - variable YT is not defined
提问by mheavers
I'm creating a youtube player embed via the YT api but I keep getting an alert that the variable YT is not defined. I can see that the script for the youtube API is getting included, which is supposed to create the variable YT - so why isn't this working? It works elsewhere on my site.
我正在创建一个通过 YT api 嵌入的 youtube 播放器,但我不断收到警告,指出变量 YT 未定义。我可以看到包含 youtube API 的脚本,它应该创建变量 YT - 那么为什么这不起作用?它适用于我网站的其他地方。
Here's the link:
这是链接:
http://oncreativity.tv/site/single/4/7CtQaTmEuWk
http://oncreative.tv/site/single/4/7CtQaTmEuWk
and my code:
和我的代码:
<script>
$(document).ready(function() {
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;
var videoSupport = Modernizr.video;
var ua = navigator.userAgent.toLowerCase();
var vid = {};
var params = { allowScriptAccess: "always" };
var atts = { id: "video_player_flash" };
{exp:channel:entries channel="creators" dynamic="off" entry_id="{segment_3}" sort="asc" limit="1"}
vid.description = "{creator_description}";
vid.videoID = '{segment_4}';
vid.link = encodeURI("{creator_link}");
vid.title = "{title}";
vid.photos = [];
{creator_work}
vid.photos[{row_index}] = {};
vid.photos[{row_index}].url = "{work_img}";
vid.photos[{row_index}].title = "{work_title}";
{/creator_work}
{/exp:channel:entries}
var $vidContainerRef = $('#video_player_container');
var $vidPlayer = $('<div id="video_player"/>');
$vidContainerRef.append($vidPlayer);
vidID = vid.videoID;
player = new YT.Player('video_player', {
width: '768',
height: '432',
videoId: vidID,
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
});
</script>
回答by Andy
You'll need to wrap the YT call in a function and call it when the script is included. Or you can add the script from the file instead of calling that script to include another script.
您需要将 YT 调用包装在一个函数中,并在包含脚本时调用它。或者,您可以从文件中添加脚本,而不是调用该脚本以包含另一个脚本。
function doYT(){
window.player = new YT.Player('video_player', {
width: '768',
height: '432',
videoId: vidID,
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
}
}
window.YT && doYT() || function(){
var a=document.createElement("script");
a.setAttribute("type","text/javascript");
a.setAttribute("src","http://www.youtube.com/player_api");
a.onload=doYT;
a.onreadystatechange=function(){
if (this.readyState=="complete"||this.readyState=="loaded") doYT()
};
(document.getElementsByTagName("head")[0]||document.documentElement).appendChild(a)
}();
回答by Justin
This is the method I like best. Uses jQuery FYI.
这是我最喜欢的方法。使用 jQuery 仅供参考。
var player = {
playVideo: function(container, videoId) {
if (typeof(YT) == 'undefined' || typeof(YT.Player) == 'undefined') {
window.onYouTubePlayerAPIReady = function() {
player.loadPlayer(container, videoId);
};
$.getScript('//www.youtube.com/player_api');
} else {
player.loadPlayer(container, videoId);
}
},
loadPlayer: function(container, videoId) {
window.myPlayer = new YT.Player(container, {
playerVars: {
modestbranding: 1,
rel: 0,
showinfo: 0,
autoplay: 1
},
height: 200,
width: 200,
videoId: videoId,
events: {
'onStateChange': onPlayerStateChange
}
});
}
};
var containerId = 'ytplayer';
var videoId = 'abc123';
player.playVideo(containerId, videoId);