onYouTubeIframeAPIReady called once but multiple videos needed on a page

前端 未结 3 1182
猫巷女王i
猫巷女王i 2020-12-11 21:04

I am using a server-side method to drop in YouTube videos with playlists and functioning buttons (think of a website widget that can be called anyway on a page, and potentia

3条回答
  •  情书的邮戳
    2020-12-11 21:40

    The way I solved this in the end was by allowing each server side widget included on the page to add the information to a global javascript array. They I used the onYouTubeIframeAPIReady() function to loop over that array to produce instantiate the YT players in turn.

    /* the Global array to hold all my stuff */
    var new_player_attributes = new Array();
    function onYouTubeIframeAPIReady() {
        for(key in new_player_attributes) {
            var player = new YT.Player(key, new_player_attributes[key]);
        }
    }
    

    How one goes about formatting this array is a trivial point. It is populated from javascript output to the document from the server side include. This function above and all the other generic utility functions that control the button etc are included only once. Only the definitions of the video parameters/playlist are the only bits included per interaction of the server side loop.

提交回复
热议问题