Click here to Skip to main content
Click here to Skip to main content

How To Adjust IFrame Height on Its Content

By , 6 Jul 2007
 

Introduction

Some browsers do not support all W3C standards and you will face a problem when you want to adjust IFrame height on its content using JavaScript. I have spent some hours experimenting with different DOM-objects and I have found quite a good solution for Mozilla, Internet Explorer and Opera browsers.

Using the Code

If you have IFrame in your HTML, the first step is to add onload handler as follows:

<iframe id='myFrame' 

frameborder=0 scrolling=no width=100%  

src="..."
    onload='adjustMyFrameSize

();'> 
</iframe> 

Then you need to place such a script BEFORE the IFrame element:

 <script type="text/javascript">
    
    function getElement(aID)
    {
        return (document.getElementById) ?
            document.getElementById(aID) : 

document.all[aID];
    }

    function getIFrameDocument(aID){ 
        var rv = null; 
        var frame=getElement(aID);
        // if contentDocument exists, W3C 

compliant (e.g. Mozilla) 
        if (frame.contentDocument)
            rv = frame.contentDocument;
        else // bad Internet Explorer  ;)
            rv = document.frames[aID].document;
        return rv;
    }

    function adjustMyFrameHeight()
    {
        var frame = getElement

("myFrame");
        var frameDoc = getIFrameDocument

("myFrame");
        frame.height = frameDoc.body.offsetHeight;
    }
</script> 

History

  • 6th July, 2007: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

meglio
Web Developer
Ukraine Ukraine
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 4memberamanalam14 Oct '10 - 3:04 
Work fine for me Smile | :)
QuestionDoesn't work?memberBen Adler14 Sep '10 - 7:52 
Copied all the code, fixed typo, not run at server, iframe shows and javascript completes with no js errors, but fix is not there.
 
What gives?
Generalserver iframememberPavel Yermalovich10 May '10 - 11:16 
it's quite simple ))
protected override void OnInit(EventArgs e)
        {
            frmLection.Attributes.Add("onload", "adjustMyFrameHeight();");
            base.OnInit(e);
        }

Generalrunat="server"memberPavel Yermalovich10 May '10 - 11:09 
it works properly when i use iframe without runat="server"
Generalrunat="server"memberPavel Yermalovich10 May '10 - 11:06 
What do i need to do if i want my iframe to be server control? i have got error
 
Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 
 
Compiler Error Message: CS1026: ) expected
 
Source Error:
 
 
 
Line 53:         </tr>
Line 54:     </table>
Line 55:     <iframe id="frmLection" frameborder="0" width="100%" scrolling="no"
Line 56:                         src="Lection1.html"  onload='adjustMyFrameHeight();' runat="server"
Line 57:                         >
 

GeneralOnly IEmemberDanGetz22 Aug '09 - 20:47 
Follow up on this, can't get it to work with FireFox... it doesn't like frameDoc.body in adjustMyFrameHeight()
GeneralI had to make a change for IE8memberDanGetz22 Aug '09 - 19:55 
Besides the adjustMyFrameHeight name someone else mentioned, I had to edit the line below else // bad Internet Explorer Wink | ;)
 

function getIFrameDocument(aID){
var rv = null;
var frame=getElement(aID);

// if contentDocument exists, W3C compliant (e.g. Mozilla)
if (frame.contentDocument)
rv = frame.contentDocument;
else // bad Internet Explorer ;)
rv = document.getElementById(aID).document;
return rv;
}

GeneralGreat JobmemberMember 39633857 Jul '09 - 5:09 
Thanks so much. This was a big help. Some of the other messages were helpful as well. I have one code update that makes this work as well in IE as it does in Mozilla (Firefox) - of course, you can use this in framename-argument version as well:
 
function adjustMyFrameHeight()
{
var frame = getElement("myFrame");
var frameDoc = getIFrameDocument("myFrame");
if (navigator.appName == "Microsoft Internet Explorer")
frame.style.height = frameDoc.body.scrollHeight+'px';
else
frame.style.height = frameDoc.body.offsetHeight+'px';
}
GeneralDoesn't get smallermemberpinow17 Apr '09 - 0:45 
The iframe keeps the height of the biggest page loaded.
 
Example:
So if the first page is 800px high, then the iframe also becomes 800px.
But if the second page is only 400px then the iframe remains 800px.
 
Hope you can help me with this.
 

Btw: I also had to replace "offsetHeight" with scrollHeight to get it to work in IE7.
Generalfunction improvementmemberlanello9 Mar '09 - 2:19 
hi,
 
thanks for your grat work, but i have an idea for improve your code!
 
function adjustMyFrameHeight(framename)
{
var frame = getElement(framename);
var frameDoc = getIFrameDocument(framename);
frame.height = frameDoc.body.offsetHeight;
}
 
Wink | ;)
GeneralSmall TypomemberThe JZ28 Oct '08 - 8:17 
The call to adjustMyFrameSize(); should be adjustMyFrameHeight();, or vice-versa. Thanks for posting this - it helped me out.
Generalissuemember_test_123_16 Jul '07 - 22:35 
Beside from function name confusion, height is a style and offsetHeight doesn't work in IE, so in order to get the code above to work one should better type:
"frame.style.height = frameDoc.body.scrollHeight+'px';"
regards
 

 

 

 

GeneralRe: issuemembermeglio16 Jul '07 - 23:29 
I have tested my code on Mozilla, IE and Opera - it works.
 
Anton

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 6 Jul 2007
Article Copyright 2007 by meglio
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid