最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Stop all YouTube iframe embeds from playing? - Stack Overflow

programmeradmin3浏览0评论

I'm stuck! I'm using jquery to, on click, pull a youtube json-c feed, grab the first id, and use it to prepend the html5 iframe video to the top of the ul. I then increment it and pull the second id, third id, etc and prepend them accordingly.

The problem is, when a new video is prepended, I want any of the previous videos to stop playing. Is their any way to acplish this with javascript/jquery?

Here's the function call:

function videoCall(i) {
$.getJSON(';max-results=50&alt=jsonc', function(vid) { 
    var fullItem ='';
    var videoID = (vid.data.items[i].id);
    var videoLink = '=' + videoID;
    var videoTitle = (vid.data.items[i].title);

    fullItem += '<li class="videoItem">';
    fullItem += '<iframe title="YouTube video player" width="900" height="536" src="/' + videoID + '" frameborder="0" allowfullscreen></iframe>';
    fullItem += '<a href="mailto:?subject=ThinkerBot Video&body=' + videoLink + '">Email This</a>';
    fullItem += '</li>';

    $(fullItem).hide().prependTo('#content ul').slideDown('slow');
});
}

I'm stuck! I'm using jquery to, on click, pull a youtube json-c feed, grab the first id, and use it to prepend the html5 iframe video to the top of the ul. I then increment it and pull the second id, third id, etc and prepend them accordingly.

The problem is, when a new video is prepended, I want any of the previous videos to stop playing. Is their any way to acplish this with javascript/jquery?

Here's the function call:

function videoCall(i) {
$.getJSON('http://gdata.youtube./feeds/api/standardfeeds/most_recent?v=2&max-results=50&alt=jsonc', function(vid) { 
    var fullItem ='';
    var videoID = (vid.data.items[i].id);
    var videoLink = 'http://www.youtube./watch?v=' + videoID;
    var videoTitle = (vid.data.items[i].title);

    fullItem += '<li class="videoItem">';
    fullItem += '<iframe title="YouTube video player" width="900" height="536" src="http://www.youtube./embed/' + videoID + '" frameborder="0" allowfullscreen></iframe>';
    fullItem += '<a href="mailto:?subject=ThinkerBot Video&body=' + videoLink + '">Email This</a>';
    fullItem += '</li>';

    $(fullItem).hide().prependTo('#content ul').slideDown('slow');
});
}
Share Improve this question asked Mar 31, 2011 at 17:28 RustyEightRustyEight 911 gold badge1 silver badge2 bronze badges 1
  • add class to iframe get the iframe src delete and again put the src it will stop the video – pareshm Commented Mar 16, 2016 at 5:03
Add a ment  | 

1 Answer 1

Reset to default 2

You should be able to do something like this:

function players(func, args) {
    var iframes = document.getElementsByTagName('iframe');
    for (var i=0; i<iframes.length; ++i) {
        if (iframes[i]) {
            var src = iframes[i].getAttribute('src');
            if (src) {
                if (src.indexOf('youtube./embed') != -1) {
                    iframes[i].contentWindow.postMessage(JSON.stringify({'event': 'mand','func': func,'args': args || []}), "*");
                }
            }
        }
    }
}

// example usage
players('stopVideo');
发布评论

评论列表(0)

  1. 暂无评论