Michael
Michael

Reputation: 1204

Youtube javascript API (& ASP) with multiple videos

I've got ASP generating the embed code on my page as follows:

Table1.Rows(0).Cells(0).Text = "<object style=""height: 390px; width: 640px"" id=""ytplayer_object_left"">" & _
        "<param name=""movie"" value=""https://www.youtube.com/v/UASyS-jUKKI?version=3&feature=player_embedded&controls=0&enablejsapi=1&showinfo=0&iv_load_policy=3&playerapiid=ytplayer_left"">" & _
        "<param name=""allowfullscreen"" value=""true"">" & _
        "<param name=""allowscriptaccess"" value=""always"">" & _
        "<embed src=""https://www.youtube.com/v/UASyS-jUKKI?version=3&feature=player_embedded&controls=0&enablejsapi=1&showinfo=0&iv_load_policy=3&playerapiid=ytplayer_left"" type=""application/x-shockwave-flash"" allowfullscreen=""true"" allowscriptaccess=""always"" width=""425"" height=""344"" id=""ytplayer_left""></object>"



    Table1.Rows(0).Cells(2).Text = "<object style=""height: 390px; width: 640px"" id=""ytplayer_object_right"">" & _
        "<param name=""movie"" value=""https://www.youtube.com/v/uasys-jukki?version=3&feature=player_embedded&controls=0&enablejsapi=1&showinfo=0&iv_load_policy=3&playerapiid=ytplayer_right"">" & _
        "<param name=""allowfullscreen"" value=""true"">" & _
        "<param name=""allowscriptaccess"" value=""always"">" & _
        "<embed src=""https://www.youtube.com/v/uasys-jukki?version=3&feature=player_embedded&controls=0&enablejsapi=1&showinfo=0&iv_load_policy=3&playerapiid=ytplayer_right"" type=""application/x-shockwave-flash"" allowfullscreen=""true"" allowscriptaccess=""always"" width=""425"" height=""344"" id=""ytplayer_right""></object>"

I've made sure that the IDs are different on both, and the onYouTubePlayerReady(id) is correctly called for both (returning the 'ytplayer_left' and 'ytplayer_right'). However, I can't get each video as a different javascript variable so I can play/pause them independently.

This is the code I've tried:

var vid_left;
    var vid_right;
    function onYouTubePlayerReady(id) {
        alert(id);
        if (id = 'ytplayer_right') {
            vid_right = document.getelementbyId(id);
            alert(vid_right);
        }

        if(id = 'ytplayer_left') {
            vid_left = document.getelementbyId(id);
            alert(vid_left);
        }

Any help? Thanks.

Upvotes: 1

Views: 470

Answers (2)

user587174
user587174

Reputation: 191

I'm not sure you can get the tag with getElementById. Try:

var object = document.getElementById(objectID)

var embed = object.getElementsByTagName('embed')[0];

Upvotes: 0

Rob W
Rob W

Reputation: 348962

Two problems:

Code:

var vid_left, vid_right;
function onYouTubePlayerReady(id) {
    alert(id);
    if (id == 'ytplayer_right') {
        vid_right = document.getElementById(id);
        alert(vid_right);
    } else if(id == 'ytplayer_left') {
        vid_left = document.getElementById(id);
        alert(vid_left);
    }
}

Upvotes: 1

Related Questions