Stop YouTube video within iFrame on external Button click

前端 未结 3 2041
予麋鹿
予麋鹿 2021-02-12 17:43

I know there are similar questions to this, but I haven\'t quite been able to get it to work.

This code shows a YouTube video which is within an iframe. When a button (

相关标签:
3条回答
  • 2021-02-12 18:14

    Well, you just need to change the source (src) attribute value of iframe tag. It's that simple! :)

    With javascript, you can do it as follows:

    var stopButton = document.getElementById('video-close-button-id');
    
    stopButton.onclick = function() {
      var myPlayer = document.getElementById("youtube-player-id"); 
      myPlayer.setAttribute("src", " ");
    }; 
    

    Happy coding.

    0 讨论(0)
  • 2021-02-12 18:17

    //in HTML

    <!--the button -->
    <a class="button" data-open="videoModal" href="#">Example Video Modal</a>
    <!--Modal Video -->
    <div class="row" id="theVideo">
        <div id="videoModal" class="reveal" data-reveal data-close-on-click="true">
        <h2>This modal has video</h2>
            <div class="flex-video">
                <iframe id="youtubePlayer" width="854" height="480" src="https://www.youtube.com/embed/4z6aSO05YHg" frameborder="0" allowfullscreen allowscriptaccess="always"></iframe>
            </div>
            <button class="close-button" data-close aria-label="Close reveal" type="button" onClick="destroyVideo()">
            <span aria-hidden="true">&times;</span>
            </button>
        </div>
    </div>
    

    //in apps.js

    function destroyVideo(){
        var url = $('#youtubePlayer').attr('src');
        $('#youtubePlayer').attr('src', '');
        $('#youtubePlayer').attr('src', url);
    }
    

    //This work for Foundation 6.2

    0 讨论(0)
  • 2021-02-12 18:21

    You need to reset the link of the video, i had a similar problem this is how i solved it, i used jQuery in the process.

    //First get the  iframe URL
    var url = $('#YourIFrameID').attr('src');
    
    //Then assign the src to null, this then stops the video been playing
    $('#YourIFrameID').attr('src', '');
    
    // Finally you reasign the URL back to your iframe, so when you hide and load it again you still have the link
    $('#YourIFrameID').attr('src', url);
    

    Check this answer

    0 讨论(0)
提交回复
热议问题