D
D
Denis Rybin2015-07-23 18:38:14
YouTube
Denis Rybin, 2015-07-23 18:38:14

How to embed multiple youtube videos on one page of a website?

You need to insert several videos from youtube on one page of the site, one video can be inserted (I do it according to the documentation), and when I insert a second video on the page, a frame is displayed for only one, the last video.
id blocks are different - player1 and player2
the script itself:
var tag = document.createElement('script');
tag.src = " https://www.youtube.com/iframe_api ";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
function onYouTubeIframeAPIReady() {
var player1;
player1 = new YT.Player('player1', {
videoId: 'here is the id of the first video',
});
vartag = document.createElement('script');
tag.src = " https://www.youtube.com/iframe_api ";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
function onYouTubeIframeAPIReady() {
var player2;
player2 = new YT.Player('player2', {
videoId: 'another video id here',
});
}
What did I do wrong?
Only the second video is displayed.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey Levchenko, 2017-05-21
@RybinDen

The question is old, but recently I myself needed to do this and the search brought me here. Here is a working version:

<div id="video0"></div>
<div id="video1"></div>

var tag = document.createElement('script');
    tag.src = "https://www.youtube.com/iframe_api";
    var firstScriptTag = document.getElementsByTagName('script')[0];
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
    var player = [];
    function onYouTubeIframeAPIReady() {
            player[0] = new YT.Player('video0', {
                height: '360',
                width: '640',
                videoId: 'id_video0',
            });
            player[1] = new YT.Player('video1', {
                height: '360',
                width: '640',
                videoId: 'id_video1',
            });
    }

D
Denis Ineshin, 2015-07-23
@IonDen

Most likely you just use the same id for containers or something like that. Make sure that each block where you want to embed the video is unique.

D
Denis Rybin, 2015-07-23
@RybinDen

I make different blocks:
div id="player1" and
div id="player2"
In the onYouTubeIframeAPIReady function, I also specify different id.
Now I noticed that I have one and the same variable var player in these functions;
Do they need to be specified as well?
I tried different ones, didn't help.

N
noys, 2015-07-23
@noys

via iframe?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question