views:

706

answers:

3

Hello All,

I can see that this question has been asked several times, but none of the proposed solutions seem to work for the site I am building, so I am reopening the thread. I am attempting to size an iframe based on the height of it's content. Both the page that contains the iframe and it's source page exist on the same domain.

I have tried the proposed solutions in each of the following threads:

I believe that the solutions above are not working because of when the reference to body.clientHeight is made, the browser has not actually determined the height of the document.

Here is the code I am using:

    var ifmBlue = document.getElementById("ifmBlue");
    ifmBlue.onload = resizeIframe;

    function resizeIframe()
    {
        var ifmBlue = document.getElementById("ifmBluePill");
        var ifmDiv = ifmBlue.contentDocument.getElementById("main");
        var height = ifmDiv.clientHeight;
        ifmBlue.style.height = (ifmBlue.contentDocument.body.scrollHeight || ifmBlue.contentDocument.body.offsetHeight || ifmBlue.contentDocument.body.parentNode.clientHeight || height ||  500) + 5 + 'px';
    }

If I debug the script using fire debug, the client height of the iframe.contentDocument's main div is 0. Additionally, body.offsetHieght, & body.scrollHeight are 0. However, after the script is finished running, if I inspect the DOM of the HTML iframe element (using fire debug) I can see that the body's clientHeight is 456 and the inner div's clientHeight is 742. This leads me to believe that these values are not yet set when iframe.onload is fired. So, per one of the threads above, I moved the code into the body.onload event handler of the iframe's source page. This solution also did not work.

Any help you can provide is much appreciated.

Thanks,

CJ

+1  A: 

you moved the handler? maybe you should move the function to the inner frame as well, so that when you grab height values you reference the body directly rather than frame object... then call a parent.set height function

another trick, call function after settimeout of 10 msecs

i remember I had that problem once but I used IE's getBoundingClientRect() to get height of content, check mozilla developer center for something similar, this is just a hint, i did not research it

on another note, what is ifmBluePill? is it the iframe? or a div inside of it? why do you reference "contentDocument" of a div?

Ayyash
+3  A: 

I remember a script on DynamicDrive that did something like that.

Where was it?

Oh, here.

a2h
There's an updated version of the script here: http://www.dynamicdrive.com/dynamicindex17/iframessi2.htm
Scott Stafford
+1  A: 

By the way, DynamicDrive improved their script to always resize even if the iframe contents change: http://www.dynamicdrive.com/dynamicindex17/iframessi2.htm

From their page:

This is version II of the original Iframe SSI script, which like the original script lets you seamlessly display external content on your page via an IFRAME. It does this by dynamically resizing the IFRAME to be the height of the page contained within it, eliminating any possible IFRAME scrollbars from appearing while snugly showing the entire external content. Think of it as SSI (server side includes) emulated using DHTML! This script works in both IE5+ and NS6+, and for other browsers, supports the option to either completely hide the iframe in question or display it using its default height.

Now, this script differs from the original in that you can load additional documents* into the IFRAME even after the page has loaded, and the IFRAME will dynamically adjust its height to fit the new document. So use this script if you need to not only display external content via the IFRAME tag, but intend to change this content after the page has loaded.

Scott Stafford