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?

5
  • possible duplicate of stackoverflow.com/questions/926916/… Commented Jun 15, 2012 at 15:49
  • Sounds very much like this: stackoverflow.com/questions/1088544/… Commented Jun 15, 2012 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. Commented Jun 15, 2012 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. Commented Jun 15, 2012 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. Commented Jun 15, 2012 at 16:11

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I get "null" or "undefined" when I try either of those. I also get a "Unsafe JavaScript attempt to access frame" error
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.)
Yea this is it. The frame is cross-domain. Thanks!
2

Try this :

var a = document.getElementById("iframe2").getElementsByTagName("*")[0];

Comments

2

Please try this code:

var a = document.getElementById("iframe2").firstChild;

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.