2

I need to root out iframes nested within iframes nested within iframes.

The structure for my code is shown below. It works fine in all browsers but in ie it results in an infinite loop. However, the cpu doesn't get raised at all while it is in the infinite loop.

var getNested_iframes = function (document_element) {
        $.each(document_element.find("iframe"), function () {
            getNested_iframes($($(this)[0].document));
        });
        alert(".");
    }
    getNested_iframes($(myWindow._element));
    alert("done");

1 Answers1

0

I'm not quite sure why you're doing it just like you're doing it, but the "$($(this)[0].document)" bit looks a tch fishy to me. In any case, it's worth noting that jquery objects are essentially lists of arbitrary size, so you can do this with iteration, rather than recursion (and swapping from recursion to iteration, when you can do it, often causes problems like this to simplify or clear up). Try the following

$startlist = $(myWindow._element);

while($startlist.length > 0)
{
    $nextlist = $startlist.find("iframe");
    alert("(" + nextlist.length + ")");
    $startlist = $nextlist;
}

alert("done");

It even lets you track how many of them are at each level, how many levels there are, and so forth.

Ben Barden
  • 2,001
  • 2
  • 20
  • 28
  • Yes! that worked. I took a screenshot of the debugger where the error was occurring. I might be reading the debuggers wrong but it looks like IE has the object in question inside of another object. http://i1120.photobucket.com/albums/l493/powerfulcrunch/whatthehell.png –  Jan 26 '12 at 20:54
  • 1
    IE has a habit of adding dom object wrappers in places that you never intended. I've particularly noticed it around tables, but that certainly isn't the only place it crops up. – Ben Barden Jan 26 '12 at 20:59
  • ah no wait! That does work but it only goes down one level. I'm in a situation with nested iframes so I need the recursion. –  Jan 26 '12 at 21:08
  • wait again... thats a pretty stupid idea to attempt getting nested iframes because the content of the nested iframes is not available to you since it comes from src. ie doesnt support window events. im not sure if this one is solvable. –  Jan 27 '12 at 14:13
  • http://stackoverflow.com/questions/364952/jquery-javascript-accessing-contents-of-an-iframe looks like it might have your answer one way or the other. Looks like there are some cases where you can touch the inside of an iframe, and some where you just can't. – Ben Barden Jan 30 '12 at 22:36
  • First read-over suggests that the major barriers to entry are the same origin policy (ie, if the iframe isn't same protocol, host, and port, you can't do a thing to it) and waiting for the iframes to finish loading before you start counting. – Ben Barden Jan 30 '12 at 22:43