adnan
adnan

Reputation: 1385

Youtube pause all iframes with same class

I want to pause all the youtube movies (independent of it's ID) with a simple onclick. I've made a jsfiddle for the code that would work with a certain ID. However since I can't assign unique ID's I want to be able to pause all the youtube iframes. The iframes do however have a class. http://jsfiddle.net/4bPAJ/3/ Any help in the right direction is appreciated :)

Cheers, Adnan

Javascript:

function callPlayer(e, t, n) {
    function o(e, t) {
        var n = e ? window.addEventListener : window.removeEventListener;
        n ? n("message", t, !1) : (e ? window.attachEvent : window.detachEvent)("onmessage", t)
    }
    if (window.jQuery && e instanceof jQuery) e = e.get(0).id;
    var r = document.getElementById(e);
    if (r && r.tagName.toUpperCase() != "IFRAME") {
        r = r.getElementsByTagName("iframe")[0]
    }
    if (!callPlayer.queue) callPlayer.queue = {};
    var i = callPlayer.queue[e],
        s = document.readyState == "complete";
    if (s && !r) {
        window.console && console.log("callPlayer: Frame not found; id=" + e);
        if (i) clearInterval(i.poller)
    } else if (t === "listening") {
        if (r && r.contentWindow) {
            t = '{"event":"listening","id":' + JSON.stringify("" + e) + "}";
            r.contentWindow.postMessage(t, "*")
        }
    } else if (!s || r && (!r.contentWindow || i && !i.ready)) {
        if (!i) i = callPlayer.queue[e] = [];
        i.push([t, n]);
        if (!("poller" in i)) {
            i.poller = setInterval(function () {
                callPlayer(e, "listening")
            }, 250);
            o(1, function u(t) {
                var n = JSON.parse(t.data);
                if (n && n.id == e && n.event == "onReady") {
                    clearInterval(i.poller);
                    i.ready = true;
                    o(0, u);
                    while (n = i.shift()) {
                        callPlayer(e, n[0], n[1])
                    }
                }
            }, false)
        }
    } else if (r && r.contentWindow) {
        if (t.call) return t();
        r.contentWindow.postMessage(JSON.stringify({
            event: "command",
            func: t,
            args: n || [],
            id: e
        }), "*")
    }
}
callPlayer("whateverID", "playVideo")

Upvotes: 1

Views: 424

Answers (1)

Hanlet Escaño
Hanlet Escaño

Reputation: 17370

Try this:

$(document).ready(function () {
    var i = 0;
    $(".youtubeplayer").each(function () {
        $(this).attr("id", "youtubeplayer" + i);
        callPlayer("youtubeplayer" + i, "playVideo");
        i++;
    });
});

This will add an id to each frame, and call the function afterwords.

Upvotes: 1

Related Questions