javascript / youtube api - 未定义变量YT [英] javascript / youtube api - variable YT is not defined

查看:123
本文介绍了javascript / youtube api - 未定义变量YT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个通过YT api嵌入的youtube播放器,但我一直收到一个警告,即没有定义变量YT。我可以看到youtube API的脚本被包含在内,它应该创建变量YT - 所以为什么这不起作用?它适用于我网站上的其他地方。

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.

这是链接:

http://oncreativity.tv/site/single/4/7CtQaTmEuWk

和我的代码:

<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>


推荐答案

您需要将YT呼叫包裹在函数并在包含脚本时调用它。或者您可以从文件中添加脚本,而不是调用该脚本以包含其他脚本。

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.

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)
}();

这篇关于javascript / youtube api - 未定义变量YT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆