I am creating a website with bootstrap that can be found here - http://www.branchingouteurope.com/digital-spark-testing/
If you select the video image you'll see a modal window open a youtube video. The problem I have is that when the modal window is closed the video keeps playing. I want this video to stop when the modal window is closed.
I've looked online and read many solutions however none seem to work. Here is the mark up...
<span class="main_video"> <div data-toggle="modal" data-target="#myModal" style="cursor:pointer"> <img src="img/master/video.fw.png" height="81%" width="60%" alt="Digital Spark Video"/> </div> </span> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> </div> <div class="modal-body"> <iframe width="100%" height="100%" src="//www.youtube.com/embed/sIFYPQjYhv8?rel=0" frameborder="0" allowfullscreen></iframe> </div> </div>
`
Here's a little bit lighter of an answer based on dizarter's version and one of the solutions he links to.
$('#modal-video').on('hidden.bs.modal', function () { $("#modal-video iframe").attr("src", $("#modal-video iframe").attr("src")); });
Testing here in FireFox 26.0, works as expected. When I either close the Modal or click outside of it and then re-open it - video is back to start and stopped.
Reproduced in Chrome, the video indeed keeps playing after the Modal is closed.
Try these already answered questions
Stop a youtube video with jquery?
Twitter Bootstrap Modal stop Youtube video
The best way seems to be to use YouTube API to stop the video. Answer that uses this method is available in the questions above.
This solution seems to be working.
First, get the JS from this post: YouTube iframe API: how do I control a iframe player that's already in the HTML? and include it on the page.
Add this JS after you have loaded the above script (just before the closing body tag)
<script type="text/javascript"> $('#myModal').on('hidden.bs.modal', function () { callPlayer('yt-player', 'stopVideo'); }); </script>
You will also need to add an ID to the div containing the iframe, as below
<div class="modal-body" id="yt-player"> <iframe src="//www.youtube.com/embed/sIFYPQjYhv8?rel=0&enablejsapi=1" allowfullscreen="" width="100%" frameborder="0" height="100%"></iframe> </div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With