Capturing innerHTML of an iFRAME's content

Is it possible for the parent of an iFRAME to extract the HTML of the iFRAME’s source document? I stumbled across a code faq that stated this was the way to do it:

var content = document.getElementById('myIFrame').document.innerHTML;

however this gave me undefined.

when I add the body reference like so:

var content = document.getElementById('myIFrame').document.body.innerHTML;

…I get the HTML of the parent instead of the iFRAME content that I wish to reference. I have also tried:

var content = document.getElementById('myIFrame').document.documentElement.innerHTML;

and

var content = document.getElementById('myIFrame').documentElement.innerHTML;

which results in the parent’s HTML and an ‘object null or not found’ exception, respectively.

Is this possible to do? and if so what is the proper path?

Thanks

-Andrew

ie u want show the code of a frame into a textarea with id hey…


document.getElementById("hey").innerHTML = document.getElementById("myFrame").body;

cheers :slight_smile:

I am trying to capture as a string (with which i can do parsing & comparisons) the HTML (not the rendered content) of an I-FRAME(not a standard frame)'s content.

Your reference gives a result of undefined.

Hi,

It is kind of a strange distinction, but

document.getElementById(‘myIFrame’)

gets you a reference to the iframe element. The iframe element on your page looks something like this:

<iframe src=“somepage.htm” style=“width:100; height:100”>
</iframe>

and as you can see there is no innerHTML–instead there is nothing between the opening and closing iframe tags. What you need to do is get a reference to the iframe window, which is done like this:

document.frames[“myIFrame”]

Then, since innnerHTML grabs whatever is between the opening and closing tag you mention, in that window you want to get:

document.body.innerHTML

Putting it all together results in:

document.frames[“myIFrame”].document.body.innerHTML

That’s sounds promising. Thanks for the lead!