Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I launch and close jQuery Twitter Bootstrap Modal with a YouTube video?

I need a simple demo of how to launch and close a YouTube video in a Twitter Bootstrap Modal from JavaScript (rather than an anchor tag click).

So far I can launch it just fine, but on close it keeps playing in the background.

Here's my html:

<div id="myModalThumbnail"><img src="http://placehold.it/250x150&text=Video%20Thumbnail">
</div>

<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    </div>
    <div class="modal-body">
        <iframe width="520" height="390" frameborder="0" allowfullscreen=""></iframe>
    </div>
</div>​

Javascript:

$('#myModalThumbnail').click(function () {
    var src = 'http://www.youtube.com/v/KVu3gS7iJu4&amp;rel=0&amp;autohide=1&amp;showinfo=0&amp;autoplay=1';
    $('#myModal').modal('show');
    $('#myModal iframe').attr('src', src);
});

​ And a jsfiddle: http://jsfiddle.net/VtKS8/5/

What next?

like image 636
Ryan Avatar asked Dec 17 '12 20:12

Ryan


People also ask

How do I stop video when modal window is closed?

on('modalclosed', function() { console. log("Modal is closed then stop the video"); $('#video'). trigger('pause'); });

How do you stop a video from bootstrap modal is closed?

add ID attribute on iframe. add jQuery code which will fire on modal close event. the code simply removes src attribute from iframe which stops the Youtube player.

How to use modal in Bootstrap 4?

Basic ModalCreate a modal by using the . modal class and attribute data-toggle = "modal" on a element, like a button or link, along with a data-target = "#identifier" or href = "#identifier" to target a specific modal to toggle.


1 Answers

Well, looks like all I needed to do was remove the src attribute on the button click.

$('#myModal button').click(function () {
    $('#myModal iframe').removeAttr('src');
});

JSFiddle: http://jsfiddle.net/VtKS8/6/

like image 59
Ryan Avatar answered Oct 01 '22 17:10

Ryan