【发布时间】:2015-06-22 17:53:58
【问题描述】:
我见过很多与此类似的问题,但我相信这是不同的。当我尝试在全局范围内定义onYouTubeIframeAPIReady 时,该函数仅在加载页面的大约一半时被调用(如果我不断刷新,有时我会看到控制台消息,有时它不存在)。让我感到困惑的是,这只有时会发生,而且我不会在代码中的其他任何地方调用 onYouTubeIframeAPIReady。
我检查过的问题区域:
- 我在 Github Pages 上运行它(所以它不是本地的)
- 函数在全局范围内定义
我的代码如下:
window.onYouTubeIframeAPIReady = function() {
console.log("YouTube API Ready");
player = new YT.Player('player', { // TODO: Sometimes this doesn't work
videoId: curVideoId,
playerVars: {
controls: 1,
autoplay: 0,
disablekb: 1,
enablejsapi: 1,
iv_load_policy: 3,
// modestbranding: 1,
showinfo: 1
}
});
ytLoaded = true;
if (windowWidth) { // if document loaded first
resizePlayer();
}
}
【问题讨论】:
-
我从未使用过 iframe api,相反,我总是将 iframe 插入到 dom 中。这总是有效的。
-
嗯,谢谢。我会这样做,但我需要能够操纵播放器(例如视频中的特定时间 .seekTo())
标签: javascript youtube-api youtube-javascript-api