-
-
Save johnny77221/dd49fba94330e80a8fa1ad60dd0ce7a4 to your computer and use it in GitHub Desktop.
A simple method to stop YouTube, Vimeo, and HTML5 videos from playing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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(iframe) { | |
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); |
To the best of my knowledge, if resetting iframe src does not work(iframe contains autoplay video), there is no way to do this.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it won't work with YouTube iframe.contentWindow.document - this won'r pass XSS