admin管理员组文章数量:1316030
What I want to do is:
- Wait for the document to render;
- When YouTube iframe api is ready, initialize my custom function and pass the
YT
object to it so I can build the player from inside.
This is what I have done so far. It works but I have feeling something is very wrong. I am not sure it should be done this way.
jQuery.getScript(""); // load YT api
jQuery(document).ready(function() {
onYouTubeIframeAPIReady = function() {
new my_custom_function().init(YT); // init my function and pass YT object
};
});
I'd appreciate it if someone can clarify what's the best way to do this. I do really need to build the players from inside my_custom_function()
.
What I want to do is:
- Wait for the document to render;
- When YouTube iframe api is ready, initialize my custom function and pass the
YT
object to it so I can build the player from inside.
This is what I have done so far. It works but I have feeling something is very wrong. I am not sure it should be done this way.
jQuery.getScript("http://www.youtube./iframe_api"); // load YT api
jQuery(document).ready(function() {
onYouTubeIframeAPIReady = function() {
new my_custom_function().init(YT); // init my function and pass YT object
};
});
I'd appreciate it if someone can clarify what's the best way to do this. I do really need to build the players from inside my_custom_function()
.
1 Answer
Reset to default 9As onYouTubeIframeAPIReady
function has to be in a global scope, I suppose we can't bind it in jQuery's document ready callback. One of the workarounds I see is to use jQuery deferred objects. At first we create a deffered object and resolve it in onYouTubeIframeAPIReady
callback
var YTdeferred = $.Deferred();
window.onYouTubeIframeAPIReady = function() {
YTdeferred.resolve(window.YT);
};
and then waiting for deferred object to be resolved after document ready
$(document).ready(function() {
YTdeferred.done(function(YT) {
// use YT here
});
});
See the full example on JSFiddle
本文标签: javascriptonYouTubeIframeAPIReady inside jQuery(document)readyStack Overflow
版权声明:本文标题:javascript - onYouTubeIframeAPIReady inside jQuery(document).ready - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741983747a2408552.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论