JavaScriptSource https://javascriptsource.com Search 5,000+ Free JavaScript Snippets Wed, 29 Sep 2021 22:00:57 +0000 en-US hourly 1 https://wordpress.org/?v=6.9.4 https://javascriptsource.com/wp-content/uploads/2021/07/cropped-favicon-32x32.png JavaScriptSource https://javascriptsource.com 32 32 Stop an iframe or HTML5 video from playing https://javascriptsource.com/stop-an-iframe-or-html5-video-from-playing/ Thu, 22 Jul 2021 17:59:13 +0000 https://the-js-source.flywheelsites.com/?p=106 This snippet stops or pauses YouTube, Vimeo and HTML5 videos when the content area is closed. If you’re interested in adding something like that to your scripts, here’s how it works.

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 );

Source

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

]]>