JavaScript: Iframe Auto Height

Using JavaScript to automatically adjust the height of iFrames on your page(s)

This javascript was tested on IE and FF, It works fine.

1. Html Code for IFrame

<iframe id="frameContent" name="frameContent" onload="return IframeOnload('frameContent');" class="adm_iframe" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>

2. JavaScript functions

var isIE = (navigator.appName.indexOf("Microsoft")!=-1)? true:false;
function GetDocument(frameName)
{
    
if( isIE )
    {
        
return document.frames[frameName].document;
    }
    
else
    {
        
return document.getElementById(frameName).contentDocument;
    }
};
var tm_AutoHeightFrame = null;

function AutoHeightFrame(frameName)
{
    AutoHeightFrame1(frameName);
    
if( tm_AutoHeightFrame == null )
    {
        tm_AutoHeightFrame
= setInterval(function(){AutoHeightFrame1(frameName);}, 200);
    }          
};
function AutoHeightFrame1(frameName)
{
    
var frm = document.getElementById(frameName);    
    
if( frm == null ) return;
    
var frameBody = GetDocument(frameName).body;
    
if( frameBody == null ) return;
    
var _height = frameBody.scrollHeight+10;
    
if( _height < 410 ) {_height=410;} //you can change 410 to your own value, this is min height for iframe.
    frm.height = _height;
}    
function IframeOnload(frameName)
{
    AutoHeightFrame(frameName);
};

Subscribe
Rss Feed Email Follow Us on Twitter
Search