Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return ID of all iframes in a page

Because of the widget format I'm working with I have a page which has multiple iframes embedded within iframes. I won't paste the code as it's vast and unwieldy but it is essentially just this:

<html>
    <head></head>
    <body>
        <iframe>
            <html>
                <head></head>
                <body>
                    <iframe>
                        <html>
                            <head></head>
                            <body>
                                <iframe>
                                    <html>
                                        <head></head>
                                        <body>
                                        blah
                                        </body>
                                    </html>
                                </iframe>
                            </body>
                        </html>
                    </iframe>
                </body>
            </html>
        </iframe>
    </body>
</html>

However, there may be more - or less - iframes dependent upon the page and template I use.

I'm trying to therefore get the ID of all of the iframes in this page, from within the most-embedded iframe.

Is this possible with jQuery? I've tried a few snippets of code but had no luck:

$('iframe', window.parent.document).each(function() {
    console.log($(this).attr("id"));
});

$('iframe', parent.document).each(function() {
    console.log($(this).attr("id"));
});

$('iframe').each(function() {
    console.log($(this).attr("id"));
});

The output of these is a single ID string - and unfortunately it's not the iframe I'm looking to control.

Thanks in advance,

like image 645
turbonerd Avatar asked Sep 19 '13 07:09

turbonerd


2 Answers

EDITED

This returns an iframe element which has the wanted id, and null, if the wanted id is not found.

function searchIds(wantedId) {
    var idArray = [], n,
        search = function (iframes) {
            var n;
            for (n = 0; n < iframes.length; n++) {
                if (iframes[n].frames.length > 0) {
                    search(iframes[n].frames);
                }
                idArray.push(iframes[n].frameElement.id);
                idArray.push(iframes[n].frameElement);
            }
        };
    search(window.top.frames);
    for (n = 0; n < idArray.length; n += 2) {
        if (idArray[n] === wantedId) {
            return idArray[n + 1];
        }
    }
    return null;
}

Notice, that searchIds() can't be run before onload of the main window has been fired.

like image 172
Teemu Avatar answered Sep 22 '22 13:09

Teemu


I do not believe you can reference the iframe's children directly. You will need to recursively search each iframe using it's .contents() call.

As far as I know, this will only work as long as the same-origin policy is not violated (i.e. the iframes must point to the same domain).

like image 33
Maxim Kumpan Avatar answered Sep 22 '22 13:09

Maxim Kumpan