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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With