Dynamic Iframe height using javascript
Welp another challenge another day. My good pal Scott (also at Imperium.ca) gets the credit for this one. We found while using a dotnetnuke back end it was far easier and far less limited to create our custom applications separately and include them in iframes and simply inherit the permissions from the dotnetnuke namespace in .NET. So while this worked great we had a huge draw back, the iframes were static height and although there was an “auto” setting in dotnetnuke, it did not appear to be functional in any of the browsers we tested.
Enter the dynamic iframe height.
We scoured much code for working examples but were hard pressed to find any that were cross browser compatible. We found some code at dynamicdrive.com while it did work resizing the iframes it was still missing a few things. One problem is we were not writing the “<iframe>” tag manually so we could not follow part two of the instructions, also part 3 instructs to place the resize call in an <a href> tag. Again we needed the script to automatically resize any and all iframes on the page once the child page was loaded. Scott tinkered until… in firefox IE7 and safari browsers we could add iframes and when the page fully loaded it would automatically adjust the iframe height by including one javascript file in the parent page and one javascript file in the child page.
I hope this helps someone else as much as it has helped us.
There are 2 js files that we will use the first js file goes in the parent page head block and loads the script for the parent page.
<!– BEGIN CODE –>
<script type=”text/javascript”>
/***********************************************
* IFrame SSI script II- © Dynamic Drive DHTML code library (http://www.dynamicdrive.com)
* Visit DynamicDrive.com for hundreds of original DHTML scripts
* This notice must stay intact for legal use
***********************************************/
//Input the IDs of the IFRAMES you wish to dynamically resize to match its content height:
//Separate each ID with a comma. Examples: ["myframe1", "myframe2"] or ["myframe"] or [] for none:
var iframeids=[];
//Should script hide iframe from browsers that don’t support this script (non IE5+/NS6+ browsers. Recommended):
var iframehide=”yes”
var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf(“Firefox”)).split(“/”)[1]
var FFextraHeight=parseFloat(getFFVersion)>=0.1? 16 : 0 //extra height in px to add to iframe in FireFox 1.0+ browsers
function resizeCaller() {
var dyniframe=new Array()
for (i=0; i<iframeids.length; i++){
if (document.getElementById)
resizeIframe(iframeids[i])
//reveal iframe for lower end browsers? (see var above):
if ((document.all || document.getElementById) && iframehide==”no”){
var tempobj=document.all? document.all[iframeids[i]] : document.getElementById(iframeids[i])
tempobj.style.display=”block”
tempobj.style.overflow=”visible”
}
}
}
function resizeIframe(frameid){
var buffer = 10;
var currentfr=document.getElementById(frameid)
currentfr.style.overflow=”hidden”
currentfr.scrolling=”no”
if (currentfr && !window.opera){
currentfr.style.display=”block”
if (currentfr.contentDocument && currentfr.contentDocument.body.offsetHeight) //ns6 syntax
currentfr.height = currentfr.contentDocument.body.offsetHeight+FFextraHeight + buffer;
else if (currentfr.Document && currentfr.Document.body.scrollHeight) //ie5+ syntax
currentfr.height = currentfr.Document.body.scrollHeight + buffer;
if (currentfr.addEventListener)
currentfr.addEventListener(“load”, readjustIframe, false)
else if (currentfr.attachEvent){
currentfr.detachEvent(“onload”, readjustIframe) // Bug fix line
currentfr.attachEvent(“onload”, readjustIframe)
}
}
}
function readjustIframe(loadevt) {
var crossevt=(window.event)? event : loadevt
var iframeroot=(crossevt.currentTarget)? crossevt.currentTarget : crossevt.srcElement
if (iframeroot)
resizeIframe(iframeroot.id);
}
function loadintoIframe(iframeid, url){
if (document.getElementById)
document.getElementById(iframeid).src=url
}
function stretchAllIframes() {
var objs = document.getElementsByTagName(“iframe”);
for(var i=0; i < objs.length; i++) {
iframeids[iframeids.length] = objs[i].id;
}
resizeCaller();
}
</script>
<!– END CODE –>
The second code is for the page that will be loaded into an iframe
<!– BEGIN CODE –>
<script type=”text/javascript”>
function addOnloadEvent(fnc){
if ( typeof window.addEventListener != “undefined” )
window.addEventListener( “load”, fnc, false );
else if ( typeof window.attachEvent != “undefined” ) {
window.attachEvent( “onload”, fnc );
}
else {
if ( window.onload != null ) {
var oldOnload = window.onload;
window.onload = function ( e ) {
oldOnload( e );
window[fnc]();
};
}
else
window.onload = fnc;
}
}
addOnloadEvent(parent.stretchAllIframes);
</script>
<!– END CODE–>