Sunday, January 27, 2008

How to dynamically change the height of an IFRAME in both Firefox and IE

Based on various code snippets that I found on the Internet, here is how to do it:

(Please contact me if you are the original author of the functions)



function getDocHeight(doc) {
var docHt = 0, sh, oh;
if (doc.height) {
docHt = doc.height;
}
else if (doc.body) {

if (doc.body.scrollHeight) docHt = sh = doc.body.scrollHeight;

if (doc.body.offsetHeight) docHt = oh = doc.body.offsetHeight;

if (sh && oh) docHt = Math.max(sh, oh);
}

return docHt;
}

function fnResizeIframe(sIFrameName) {

var iframeWin = window.frames[sIFrameName];

var iframeEl = window.document.getElementById? window.document.getElementById(sIFrameName): document.all? document.all[sIFrameName]: null;

if ( iframeEl && iframeWin ) {
iframeEl.style.height = "auto";
var docHt = getDocHeight(iframeWin.document);

if (docHt) iframeEl.style.height = docHt + "px";

}
else { //firefox
var docHt = window.document.getElementById(iframeName).contentDocument.height;
window.document.getElementById(iframeName).style.height = docHt + "px";
}


} //end of function getDocHeight(doc)



No comments:

Post a Comment