Skip to content

Instantly share code, notes, and snippets.

@johnny77221
Forked from cferdinandi/stop-video.js
Last active July 20, 2020 02:21
Show Gist options
  • Select an option

  • Save johnny77221/dd49fba94330e80a8fa1ad60dd0ce7a4 to your computer and use it in GitHub Desktop.

Select an option

Save johnny77221/dd49fba94330e80a8fa1ad60dd0ce7a4 to your computer and use it in GitHub Desktop.

Revisions

  1. johnny77221 revised this gist Sep 25, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion stop-video.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    * @param {Element} element The element that contains the video
    */
    var stopVideo = function ( element ) {
    element.querySelectorAll('iframe').forEach(function(item) {
    element.querySelectorAll('iframe').forEach(function(iframe) {
    if ( iframe.contentWindow ) { /* send stop to content */
    stopVideo(iframe.contentWindow.document);
    }
  2. johnny77221 revised this gist Feb 14, 2019. 1 changed file with 17 additions and 11 deletions.
    28 changes: 17 additions & 11 deletions stop-video.js
    Original file line number Diff line number Diff line change
    @@ -2,14 +2,20 @@
    * Stop an iframe or HTML5 <video> from playing
    * @param {Element} element The element that contains the video
    */
    var stopVideo = function ( element ) {
    var iframe = element.querySelector( 'iframe');
    var video = element.querySelector( 'video' );
    if ( iframe ) {
    var iframeSrc = iframe.src;
    iframe.src = iframeSrc;
    }
    if ( video ) {
    video.pause();
    }
    };
    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);
  3. Chris Ferdinandi revised this gist Jun 6, 2014. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions stop-video.js
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,15 @@
    /**
    * Stop an iframe or HTML5 <video> from playing
    * @param {Element} element The element that contains the video
    */
    var stopVideo = function ( element ) {
    var iframe = element.querySelector( 'iframe');
    var video = element.querySelector( 'video' );
    if ( iframe !== null ) {
    if ( iframe ) {
    var iframeSrc = iframe.src;
    iframe.src = iframeSrc;
    }
    if ( video !== null ) {
    if ( video ) {
    video.pause();
    }
    };
  4. Chris Ferdinandi created this gist Feb 17, 2014.
    11 changes: 11 additions & 0 deletions stop-video.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    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();
    }
    };