Skip to content

Instantly share code, notes, and snippets.

@koistya
Last active September 18, 2022 05:36
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save koistya/7eac879f674ae9f5e26c to your computer and use it in GitHub Desktop.
Save koistya/7eac879f674ae9f5e26c to your computer and use it in GitHub Desktop.
Modal dialog with a YouTube player
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
<script src="http://code.jquery.com/jquery.min.js"></script>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</head>
<body>
<!-- Open a modal dialog button -->
<a href="#" data-toggle="modal" data-target=".video-dialog">How to Use Ask the Experts</a>
<!-- Video Player Dialog -->
<div class="modal fade video-dialog" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-body">
<div id="videoDialogPlayer" class="vidflex"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script>
(function () {
var el = document.createElement('script');
el.src = "//www.youtube.com/iframe_api";
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(el, s);
var videoDialogPlayer;
window.onYouTubeIframeAPIReady = function () {
videoDialogPlayer = new YT.Player('videoDialogPlayer', {
height: '390',
width: '640',
videoId: '4HG6Ek_SyJs'
});
}
$(function () {
$('.video-dialog').on('shown.bs.modal', function () {
videoDialogPlayer.playVideo();
}).on('hide.bs.modal', function () {
videoDialogPlayer.stopVideo();
});
});
}());
</script>
</body>
</html>
@BushiNoTamashiiKen-zz
Copy link

This works excellently!
Great work.
Cheers.

@hunt0777
Copy link

Thank you sooooo much!!!!

@hunt0777
Copy link

Do you have one that works with Vimeo?

@Prashant454
Copy link

how to set autoplay???

@juagarciaga
Copy link

juagarciaga commented Sep 14, 2018

I have two modals with diferents videos on the same page and just work for the first one.
How can I make work the second one?
I was trying to do a for each but it doesnt work :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment