首先了解两个概念,contentWindow和contentDecument,可以在子级iframe设置父级和孙级iframe高度。contentWindow兼容各个浏览器,可取得子窗口的window对象;contentDocument Firefox支持,>ie8支持。可取得子窗口的document对象。
document.body有三个名词解释,clientHeight、offsetHeight和scrollHeight的解释,这里说的是document.body。如果是HTML控件,就会又不同。
clientHeight,都认为是内容可视区域的高度,也就是说页面浏览器中可以看到内容的这个区域的高度,一般是最后一个工具条以下到状态栏以上的这个区域,与页面内容无关。
而offsetHeight和scrollHeight根据浏览器不同,定义有差别,但是一般有一个固定的公式,
关系公式:scrollHeight = offsetHeight+ scrollTop
var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]; var FFextraHeight=getFFVersion>=0.1? 16 : 0; function dyniframe() { var pTar = document.getElementById("test"); var height; if (pTar && !window.opera) { pTar.style.display = "block";
if (pTar.contentDocument && pTar.contentDocument.body.offsetHeight) { pTar.height = pTar.contentDocument.body.scrollHeight; height = pTar.contentDocument.body.scrollHeight; }else if (pTar.Document && pTar.Document.body.scrollHeight) { var hei = pTar.Document.body.scrollHeight; height = hei; if (hei > 500){ pTar.height = hei; } else{ pTar.height = 500; } }else if (pTar.contentWindow && pTar.contentWindow.outerHeight) { pTar.height = pTar.contentWindow.outerHeight + FFextraHeight; } } parent.dyniframe(height); }
下面是网上找的一个自诩是完美兼容的代码,如果你用谷歌浏览器看,需要配置个环境 如IIS,得在环境下运行才能看到效果。大部分人说不能完全兼容都是这个问题。
function reinitIframe(){
var iframe = document.getElementById("frame_content");
try{
iframe.height = iframe.contentWindow.document.documentElement.scrollHeight;
}catch (ex){}
}
window.setInterval("reinitIframe()", 200);
|