Answer the question
In order to leave comments, you need to log in
How to dynamically add youtube players (iframe) to the page?
Greetings. I have a task to dynamically, by triggering certain events, add iframes with youtube videos to the page. Docks - https://developers.google.com/youtube/iframe_api_r... I read it, but one thing was not clear to me.
The examples clearly show how to load a single iframe with a video - first, the API is loaded in the script tag:
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() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
Answer the question
In order to leave comments, you need to log in
Before cycle
<div id="ytmain"></div>
<script>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() {
<?php if(!isset($lal)){$lal=0;}else{$lal++;}?>
player[<?php print $lal;?>] = new YT.Player('player<?php print $lal;?>', {
height: '494', // Высота
width: '878', // Ширина
videoId: "<?php print $video['popup'];?>", /* kaR07DuN2T8 */ // ID видео
playerVars: { 'autoplay': 1, 'loop': 1},events: {'onReady': onPlayerReady,'onStateChange': onPlayerStateChange}});
var new_input = document.createElement("div") ;
new_input.id = "player<?php print $lal;?>" ;
document.getElementById("ytmain").appendChild(new_input) ;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question