admin管理员组

文章数量:1201372

So I do this:

var stuff = document.getElementsByTagName('iframe');

Works fine. Then I want to do this:

var other = stuff.getElementsByTagName('a');

but it doesn't work. It ends up being undefined. I can do:

var other = document.getElementsByTagName('a');

and it's fine. Should I not be able to get all the 'a' in 'iframe'?

Edit: So I don't own the iframe and am now under the impression that I can't access anything the iframe generates which meansI guess I'm screwed.

So I do this:

var stuff = document.getElementsByTagName('iframe');

Works fine. Then I want to do this:

var other = stuff.getElementsByTagName('a');

but it doesn't work. It ends up being undefined. I can do:

var other = document.getElementsByTagName('a');

and it's fine. Should I not be able to get all the 'a' in 'iframe'?

Edit: So I don't own the iframe and am now under the impression that I can't access anything the iframe generates which meansI guess I'm screwed.

Share Improve this question edited Feb 20, 2012 at 4:55 townie asked Feb 20, 2012 at 4:35 townietownie 7872 gold badges8 silver badges12 bronze badges 1
  • @Anurag No. I saw someone posted an answer saying you can't access that information due to browser security but they deleted it. I guess you can only access the actual iframe part and any content it generates is off limits? – townie Commented Feb 20, 2012 at 4:49
Add a comment  | 

5 Answers 5

Reset to default 10

Use..

var iframe = document.getElementsByTagName('iframe')[0],
    iframeDoc = iframe.contentWindow.document;

Specification.

Then, you can call getElementsByTagName('a') on iframeDoc. Alternatively, you could access iframeDoc.links, which isn't quite the same but may be what you want.

Of course, this all relies on the access to the iframe's document not violating Same Origin Policy.

getElementsByTagName() returns a NodeList, which you'll need to iterate over. To iterate over it, you can do:

for (var i = 0; i < stuff.length; i++) {   
    var other = stuff[i].getElementsByTagName('a');
}  

However, getElementsByTagName('a') asks for children inside the current document, which is almost certainly not what you want. If you want children inside the document in the frame, you'll need to do:

for (var i = 0; i < stuff.length; i++) {   
    var other = stuff[i].contentWindow.document.getElementsByTagName('a');
}  

This will work if you have multiple iframes, and in older versions of IE.

stuff[0].contentWindow.document.getElementsByTagName('a')

should produce an array-like object containing all the links in the zero-th iframe.

You need to use contentWindow.document because the <iframe> does not contain any nodes in the current document -- it is a frame whose contentWindow property points to a different window with its own document.

getElementsByTagName returns an array of matching elements. To access an individual IFRAME, you would use stuff[0] for example.

Why is everyone suggesting contentWindow.document when you can just use contentDocument instead?

Of course, both work, but I prefer to use what I mean. If I want the window, I use contentWindow. If I want the document, I use contentDocument.

本文标签: javascriptgetElementsByTagName(39a39) not workingStack Overflow