Return to Snippet

Revision: 8043
at August 29, 2008 10:07 by jamesming


Initial Code
function insertIt() {
var _y = document.getElementById('framediv');
var _x = window.frames[0].document.body.innerHTML;
_y.innerHTML = _x
}
Then, the parent page has a div with the id "framediv" which is placed where the contents of the iframe should appear on the page.
The iframe element is then placed inside the div, like this:

<div id="framediv">
<iframe onload="insertIt();" src="/somewhere/content.htm" frameborder="no" width="555px" scrolling="no">
</div>

Initial URL
http://www.webmasterworld.com/forum21/7779.htm

Initial Description
Post a stripped down version of the page if you can. One that shows exactly how the iframe fits into the structure of the page.

The solution I settled on works like this:
The parent page has a javascript function like this:

function insertIt() {
var _y = document.getElementById('framediv');
var _x = window.frames[0].document.body.innerHTML;
_y.innerHTML = _x
}
Then, the parent page has a div with the id "framediv" which is placed where the contents of the iframe should appear on the page.
The iframe element is then placed inside the div, like this:

<div>
<iframe>
</div>

As you can see, there's onload="insertIt()" inside the iframe element. So when the iframe loads, the parent page immediately takes the contents of the body of the document contained in the iframe and actually replaces the iframe element with that content.

It's a little tricky to comprehend, but it works great.

You said you're a newbie, does this make any sense to you?

also, is the iframe document located on the same server and the same domain as the parent page?
That will play a part.

Initial Title
iframe resize to match frame source using javascript and div innerhtml

Initial Tags
resize

Initial Language
JavaScript