How to stop an iframe video when closing modal

Needed to stop a video from playing and was impatient and having trouble getting it to work using YouTubes APIs. Found this solution and it worked right off the bat.

https://gomakethings.com/stopping-youtube-vimeo-and-html5-videos-with-javascript/

Add this method to your script:

var stopVideo = function ( element ) {
var iframe = element.querySelector( 'iframe');
var video = element.querySelector( 'video' );
if ( iframe !== null ) {
var iframeSrc = iframe.src;
iframe.src = iframeSrc;
}
if ( video !== null ) {
video.pause();
}
};

Call the function as needed, passing the container element into the function:

stopVideo( tab );

Posted

in

by

Tags: