Autoplay YouTube Video inside Modal - HTML CSS Bootstrap

HTML CSS examples for Bootstrap:Modal

Description

Autoplay YouTube Video inside Modal

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
 <head> 
  <title>Example of Autoplaying YouTube Video inside Bootstrap Modal</title> 
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
  <style type="text/css">
    .modal-content iframe{
        margin: 0 auto;
        display: block;
    }<!--from w  w w . jav  a 2s.  c  o  m-->
</style> 
  <script type="text/javascript">
$(document).ready(function(){
    var url = $("#cartoonVideo").attr('src');
    
    $("#cartoonVideo").attr('src', '');
  
    $("#myModal").on('shown.bs.modal', function(){
        $("#cartoonVideo").attr('src', url);
    });
    
    $("#myModal").on('hide.bs.modal', function(){
        $("#cartoonVideo").attr('src', '');
    });
});
</script> 
 </head> 
 <body> 
  <div> 

   <a href="#myModal" class="btn btn-lg btn-primary" data-toggle="modal">Launch Demo Modal</a> 

   <div id="myModal" class="modal fade"> 
    <div class="modal-dialog"> 
     <div class="modal-content"> 
      <div class="modal-header"> 
       <button type="button" class="close" data-dismiss="modal" aria-hidden="true">?</button> 
       <h4 class="modal-title">YouTube Video</h4> 
      </div> 
      <div class="modal-body"> 
       <iframe id="cartoonVideo" width="560" height="315" src="http://www.youtube.com/embed/YE7VzlLtp-4?autoplay=1" frameborder="0" allowfullscreen></iframe> 
      </div> 
     </div> 
    </div> 
   </div> 
  </div>   
 </body>
</html>

Related Tutorials