Last active
December 23, 2015 22:49
-
-
Save vgrem/6705869 to your computer and use it in GitHub Desktop.
Create an embedded YouTube player that will load a video
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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