Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
451 views
in Technique[技术] by (71.8m points)

javascript / youtube api - variable YT is not defined

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.

Here's the link:

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

and my code:

<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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...