Click here to Skip to main content
Licence 
First Posted 6 Jul 2007
Views 109,910
Bookmarked 17 times

How To Adjust IFrame Height on Its Content

By meglio | 6 Jul 2007
How to adjust an iFrame height to its content: tested in Mozilla, Opera, Internet Explorer
1 vote, 8.3%
1

2
1 vote, 8.3%
3
5 votes, 41.7%
4
5 votes, 41.7%
5
4.27/5 - 12 votes
1 removed
μ 3.88, σa 2.04 [?]

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


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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 4 Pinmemberamanalam4:04 14 Oct '10  
QuestionDoesn't work? PinmemberBen Adler8:52 14 Sep '10  
Generalserver iframe PinmemberPavel Yermalovich12:16 10 May '10  
Generalrunat="server" PinmemberPavel Yermalovich12:09 10 May '10  
Generalrunat="server" PinmemberPavel Yermalovich12:06 10 May '10  
GeneralOnly IE PinmemberDanGetz21:47 22 Aug '09  
GeneralI had to make a change for IE8 PinmemberDanGetz20:55 22 Aug '09  
GeneralGreat Job PinmemberMember 39633856:09 7 Jul '09  
GeneralDoesn't get smaller Pinmemberpinow1:45 17 Apr '09  
Generalfunction improvement Pinmemberlanello3:19 9 Mar '09  
GeneralSmall Typo PinmemberThe JZ9:17 28 Oct '08  
Generalissue Pinmember_test_123_23:35 16 Jul '07  
GeneralRe: issue Pinmembermeglio0:29 17 Jul '07  

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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