Skip to content

Instantly share code, notes, and snippets.

@khripunovpp
Forked from johnny77221/stop-video.js
Created May 26, 2019 21:56
Show Gist options
  • Save khripunovpp/e1bedc8ac1ccb723517fa938855a152c to your computer and use it in GitHub Desktop.
Save khripunovpp/e1bedc8ac1ccb723517fa938855a152c to your computer and use it in GitHub Desktop.
A simple method to stop YouTube, Vimeo, and HTML5 videos from playing.
/**
* Stop an iframe or HTML5 <video> from playing
* @param {Element} element The element that contains the video
*/
var stopVideo = function ( element ) {
element.querySelectorAll('iframe').forEach(function(item) {
if ( iframe.contentWindow ) { /* send stop to content */
stopVideo(iframe.contentWindow.document);
}
else { /* Cross Domain, resetting src is all we can do, and the iframe might fail loading same url */
var iframeSrc = iframe.src;
iframe.src = iframeSrc;
}
});
element.querySelectorAll('video').forEach(function(item) { item.pause(); });
element.querySelectorAll('audio').forEach(function(item) { item.pause(); });
};
// then call from top level document
stopVideo(document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment