5

I have an iframe that looks like this:

<iframe id="iframe2" ...>
    #document
        <html>...</html>
</iframe>

I am trying to get the item underneath the iframe with the html tag.
In JavaScript, when I do:

document.getElementByID("iframe2")

this returns the correct iframe.

However, when I do this:

document.getElementByID("iframe2").childNodes

the return value is [].

document.getElementByID("iframe2").getElementsByTagName("#document") and document.getElementByID("iframe2").getElementsByTagName("html") also return [].

How do I access that html tag?
Also, what is that #document tag called?

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
Di Zou
  • 4,469
  • 13
  • 59
  • 88
  • possible duplicate of http://stackoverflow.com/questions/926916/how-to-get-body-content-of-iframe-by-javascript – kapa Jun 15 '12 at 15:49
  • Sounds very much like this: http://stackoverflow.com/questions/1088544/javascript-get-element-from-within-an-iframe – Hauke Haien Jun 15 '12 at 15:49
  • There's something wrong. What is #document exactly? Isn't it just a text? Mixing text with html tags can result in the whole content considered as a text node. – Kamyar Infinity Jun 15 '12 at 15:58
  • @Kamyar `#document` is not a real child node; it's a placeholder inserted by Chrome dev tools (possibly Firebug as well?) that represents the iframe's `contentDocument` property. – apsillers Jun 15 '12 at 16:11
  • @Kamyar #document is just what it looks like. When I do an inspect elements, there is a "#document" nested inside the iframe. – Di Zou Jun 15 '12 at 16:11

4 Answers4

6
document.getElementByID("iframe2").contentWindow.document

Or, the variant not supported by older IE,

document.getElementByID("iframe2").contentDocument

This will get you the document object of the embedded page, and from there you can use .documentElement, .body or .head properties to get the html/body/head DOM.

If you want the window object of the embedded page, use contentWindow instead of contentDocument.

MDN has a guide to iframe scripting that you will probably find helpful.

apsillers
  • 112,806
  • 17
  • 235
  • 239
  • I get "null" or "undefined" when I try either of those. I also get a "Unsafe JavaScript attempt to access frame" error – Di Zou Jun 15 '12 at 16:12
  • 4
    That means the frame is cross-domain. For security reasons, you cannot access an iframe with a cross-domain origin. (Imagine if any page on the Web could just open your Gmail inbox and search through the DOM of the page -- a security disaster.) – apsillers Jun 15 '12 at 16:15
  • Yea this is it. The frame is cross-domain. Thanks! – Di Zou Jun 15 '12 at 16:48
2

Please try this code:

var a = document.getElementById("iframe2").firstChild;
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
Bunlong
  • 652
  • 1
  • 9
  • 21
2

Try this :

var a = document.getElementById("iframe2").getElementsByTagName("*")[0];
Dave
  • 6,141
  • 2
  • 38
  • 65
Alexandre Khoury
  • 3,896
  • 5
  • 37
  • 58
0

Does the URL for the IFrame content have the same domain as the parent page? If not, you won't be able to access anything inside the IFrame due to the same-origin policy.

Matt Coughlin
  • 18,666
  • 3
  • 46
  • 59