Skip to content

Instantly share code, notes, and snippets.

@dmlap
Last active August 29, 2015 14:15
Show Gist options
  • Save dmlap/5cca8d6f88e4f44b4070 to your computer and use it in GitHub Desktop.
Save dmlap/5cca8d6f88e4f44b4070 to your computer and use it in GitHub Desktop.
Asynchronously loading the player script and creating players dynamically.
<!doctype html>
<html>
<head>
<title>Dynamic Player Loading</title>
</head>
<body>
<button id="create-a-player">Create a player!</button>
<!-- Load the player script. Note the "async" attribute -->
<script id="player-script" src="//players.brightcove.net/acctId/player_id/index.min.js" async></script>
<script>
// wait for the player script to load
document.querySelector('#player-script').onload = function() {
// whenever the user clicks on the "Create a player!" button, insert a new player into the page
document.querySelector('#create-a-player').addEventListener('click', function() {
// create the video element and insert it into the document
var video = document.createElement('video');
video.setAttribute('data-account', 'acctId');
video.setAttribute('data-player', 'player_id');
video.setAttribute('data-video-id', 'ref:video');
video.className = "video-js vjs-default-skin";
document.body.appendChild(video);
// initialize the player
bc(video);
});
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment