For a Twitter Bootstrap modal/popup with a video inside, this worked for me:
$('.modal.stop-video-on-close').on('hidden.bs.modal', function(e) {_x000D_
$('.video-to-stop', this).each(function() {_x000D_
this.contentWindow.postMessage('{"event":"command","func":"stopVideo","args":""}', '*');_x000D_
});_x000D_
});
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">_x000D_
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>_x000D_
_x000D_
<div id="vid" class="modal stop-video-on-close"_x000D_
tabindex="-1" role="dialog" aria-labelledby="Title">_x000D_
<div class="modal-dialog" role="document">_x000D_
<div class="modal-content">_x000D_
<div class="modal-header">_x000D_
<button type="button" class="close" data-dismiss="modal" aria-label="Close">_x000D_
<span aria-hidden="true">×</span>_x000D_
</button>_x000D_
<h4 class="modal-title">Title</h4>_x000D_
</div>_x000D_
<div class="modal-body">_x000D_
<iframe class="video-to-stop center-block"_x000D_
src="https://www.youtube.com/embed/3q4LzDPK6ps?enablejsapi=1&rel=0"_x000D_
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"_x000D_
frameborder="0" allowfullscreen>_x000D_
</iframe>_x000D_
</div>_x000D_
<div class="modal-footer">_x000D_
<button class="btn btn-danger waves-effect waves-light"_x000D_
data-dismiss="modal" type="button">Close</button>_x000D_
</div>_x000D_
</div>_x000D_
</div>_x000D_
</div>_x000D_
_x000D_
<button class="btn btn-success" data-toggle="modal"_x000D_
data-target="#vid" type="button">Open video modal</button>
_x000D_
Based on Marco's answer, notice that I just needed to add the enablejsapi=1
parameter to the video URL (rel=0
is just for not displaying related videos at the end). The JS postMessage
function is what does all the heavy lifting, it actually stops the video.
The snippet may not display the video due to request permissions, but in a regular browser this should work as of November of 2018.