Skip to content

Instantly share code, notes, and snippets.

@vgrem
Last active December 23, 2015 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vgrem/6705869 to your computer and use it in GitHub Desktop.
Save vgrem/6705869 to your computer and use it in GitHub Desktop.
Create an embedded YouTube player that will load a video
var ytplayersinfolist = [];
//This function creates an <iframe> (and YouTube player)
//after the API code downloads
function onYouTubeIframeAPIReady() {
if (typeof window.playerInfoList === 'undefined')
return;
for (var i = 0; i < window.playerInfoList.length; i++) {
createYTPlayer(window.playerInfoList[i]);
}
}
// This function loads the IFrame Player API code asynchronously
function registerYTIFrameApi() {
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 initYTPlayer(playerInfo) {
return new YT.Player(playerInfo.id, {
height: playerInfo.height,
width: playerInfo.width,
videoId: playerInfo.videoId
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment