Bootstrap Modal Popup Video

In many scenarios, we require a thumbnail to display on a page as an image and as we click on that image/link, we want that the related video should be played in the popup.

Here, we are playing a video on click of the link and playing that video in the bootstrap modal popup. The sample of the video can be seen as shown in the image below.



We have simply used HTML and jQuery along with the Bootstrap Modal to play the video.

HTML
  1. <div class="bs-example">  
  2.     <!-- Button HTML (to Trigger Modal) --><a href="#myModal" class="btn btn-lg btn-primary" data-toggle="modal">Launch Video Modal</a>  
  3.     <!-- Modal HTML -->  
  4.     <div id="myModal" class="modal fade">  
  5.         <div class="modal-dialog">  
  6.             <div class="modal-content">  
  7.                 <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>  
  8.                     <h4 class="modal-title">YouTube Video</h4>  
  9.                 </div>  
  10.                 <div class="modal-body"> <iframe id="cartoonVideo" autoplay width="560" height="315" src="//www.youtube.com/embed/YE7VzlLtp-4" frameborder="0" allowfullscreen></iframe> </div>  
  11.             </div>  
  12.         </div>  
  13.     </div>  
  14. </div> 
Script
  1. <script type="text/javascript">  
  2.     $(document).ready(function() {  
  3.         /* Get iframe src attribute value i.e. YouTube video url 
  4.         and store it in a variable */  
  5.         var url = $("#cartoonVideo").attr('src');  
  6.         /* Assign empty url value to the iframe src attribute when 
  7.         modal hide, which stop the video playing */  
  8.         $("#myModal").on('hide.bs.modal'function() {  
  9.             $("#cartoonVideo").attr('src''');  
  10.         });  
  11.         /* Assign the initially stored url back to the iframe src 
  12.         attribute when modal is displayed again */  
  13.         $("#myModal").on('show.bs.modal'function() {  
  14.             $("#cartoonVideo").attr('src', url);  
  15.         });  
  16.     });  
  17. </script> 
External Reference

Here, I am directly taking reference from Bootstrap website, however, you can refer them locally.
  1. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">  
  2. <!-- Optional theme -->  
  3. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">  
  4. <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>  
  5. <!-- Latest compiled and minified JavaScript -->  
  6. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>   
Style
  1. <style type="text/css">  
  2.     .modal-content iframe {  
  3.         margin: 0 auto;  
  4.         display: block;  
  5.     }  
  6. </style>  
Kindly let me know if you face issues.
Next Recommended Reading Bootstrap Modal PopUp Using ASP.NET MVC