Youtube iFrame API #39;YT is undefined#39;(Youtube iFrame API YT 未定义)
本文介绍了Youtube iFrame API 'YT 未定义'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将此代码放入模块函数中:
I'm trying to put this code in an module function:
$(document).ready(function()
{
VIDEO.onYouTubeIframeAPIReady();
}
var VIDEO = (function (my, $){
var tag = document.createElement('script');
var onPlayerStateChange;
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
my.onYouTubeIframeAPIReady =function() {
player = new YT.Player('player', {
height: '490',
width: '880',
videoId: SONG.getArtistId(),
playerVars: { controls:1, showinfo: 0, rel: 0, showsearch: 0, iv_load_policy: 3 },
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange,
'onError': catchError
}
});
}
function onPlayerReady(event) {
if(typeof(SONG.getArtistId()) == undefined)
{
console.log("undefineeeed");
}
event.target.playVideo();
}
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
// setTimeout(stopVideo, 6000);
done = true;
}
if(event.data == YT.PlayerState.ENDED)
{
location.reload();
}
}
function catchError(event)
{
if(event.data == 100) console.log("De video bestaat niet meer");
}
function stopVideo() {
player.stopVideo();
}
return my;
}(VIDEO || {}, jQuery));
问题是:即使它是自动执行的,也没有任何反应,并且 iFrame 也没有显示.我正在尝试这样做,因为它是作业的一部分.我们必须在模块中工作.
The problem is: even it's self-executing, nothing happens and the iFrame is not shown. I'm trying to do this because it's part of an assignment. We have to work in modules.
推荐答案
你需要简化代码:
HTML
<div id="player"></div>
Javascript
$(document).ready( function() {
console.log( "ready!" );
loadPlayer();
});
function getArtistId() {
return 'l-gQLqv9f4o';
}
function loadPlayer() {
if (typeof(YT) == 'undefined' || typeof(YT.Player) == 'undefined') {
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
window.onYouTubePlayerAPIReady = function() {
onYouTubePlayer();
};
} else {
onYouTubePlayer();
}
}
var player;
function onYouTubePlayer() {
player = new YT.Player('player', {
height: '490',
width: '880',
videoId: getArtistId(),
playerVars: { controls:1, showinfo: 0, rel: 0, showsearch: 0, iv_load_policy: 3 },
events: {
'onStateChange': onPlayerStateChange,
'onError': catchError
}
});
}
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
// setTimeout(stopVideo, 6000);
done = true;
} else if (event.data == YT.PlayerState.ENDED) {
location.reload();
}
}
function onPlayerReady(event) {
//if(typeof(SONG.getArtistId()) == undefined)
//{
// console.log("undefineeeed");
/
沃梦达教程
本文标题为:Youtube iFrame API 'YT 未定义'
基础教程推荐
猜你喜欢
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 直接将值设置为滑块 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01