未调用 onReady [英] onReady not being called
本文介绍了未调用 onReady的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
var player = null;函数 LoadYouTubeIframeAPI() {var scriptElement = document.createElement("script");scriptElement.src = "http://www.youtube.com/iframe_api";var firstScriptElement = document.getElementsByTagName("script")[0];firstScriptElement.parentNode.insertBefore(scriptElement, firstScriptElement);}函数 onYouTubeIframeAPIReady() {var playerParams = {玩家变量:{启用jsapi":1,来源":document.domain,相对":0},事件:{onReady":onPlayerReady,"onError": onPlayerError,"onStateChange": onPlayerStateChange}};player = new YT.Player("player", playerParams);}函数 onPlayerReady(event) {警报(准备好")}函数 onPlayerError(event) {}函数 onPlayerStateChange(event) {}
这是我的完整代码.
function onPlayerReady(event) {警报(准备好")}
永远不会因为某种原因被解雇.有人能帮忙吗?我只是无法让它工作几个小时......谷歌根本没有帮助我.
解决方案
- 确保你的播放器有一个 元素.
- 确保实际调用了函数
LoadYouTubeIframeAPI
.因为在你的代码中它永远不会被执行.您只是在定义它.- 确保您没有通过 HTTPS 访问您的网站,因为使用 javascript 加载 HTTP 资源仅适用于 HTTP 网站.
这里是一个工作示例,您可以创建一个名为
test.html
在桌面上,将内容粘贴到其中并使用网络浏览器运行它.你会看到它工作得很好.此处您可以找到 API 参考以查看更多详细信息.
var player = null; function LoadYouTubeIframeAPI() { var scriptElement = document.createElement("script"); scriptElement.src = "http://www.youtube.com/iframe_api"; var firstScriptElement = document.getElementsByTagName("script")[0]; firstScriptElement.parentNode.insertBefore(scriptElement, firstScriptElement); } function onYouTubeIframeAPIReady() { var playerParams = { playerVars: { "enablejsapi": 1, "origin": document.domain, "rel": 0 }, events: { "onReady": onPlayerReady, "onError": onPlayerError, "onStateChange": onPlayerStateChange } }; player = new YT.Player("player", playerParams); } function onPlayerReady(event) { alert("ready") } function onPlayerError(event) {} function onPlayerStateChange(event) {}
Here's my full code.
function onPlayerReady(event) { alert("ready") }
Is never getting fired for some reason. Can anyone help with this? I just can't get it to work for hours.. Google is not helping me at all.
解决方案- Make sure you have a
<div>
Element for your player. - Make sure the function
LoadYouTubeIframeAPI
is actually called. Because in your code it never gets executed. You are just defining it. - Make sure you are not accessing your site over HTTPS because loading HTTP Ressourcs with javascript only works on HTTP sites.
Here is a working example, you can just create a file called
test.html
on your desktop, paste the contents into it and run it with a webbrowser. You'll see it works just fine.Here you can find the API Reference to see some more details.
这篇关于未调用 onReady的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文 - 确保实际调用了函数