Ifram 内页面高度自适应 兼容FF IE

本文介绍了一种使IFrame自适应其内容高度的方法,并针对不同浏览器进行了优化,特别是解决了Firefox浏览器中无法正确获取body高度的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

< i frame  id=rightframe name=rightframe src="admin_right.aspx" marginWidth="10" frameSpacing="0" marginHeight="0" frameBorder="0" scrolling="no" align="center" width=785 height="1" frameborder="no" onload='this.height="500px";this.height=rightframe.document.body.scrollHeight;'   ></ i frame>

 

注:onload='this.height="500px"; 这个是为了避免有的页面很长,切到短页面,还是很长这个问题

写成JS函数形式

 

<script type="text/javascript">
function reiframe(){
var iframe=document.getElementById("dlok");
try{
var bheight=iframe.contentWindow.document.body.scrollHeight;
var dheight=iframe.contentWindow.document.documentElement.scrollHeight;
var height=Math.max(bheight,dheight);
iframe.height=height;
}catch(ex){}
}
window.setInterval("reiframe()",200);
</script>

 

 

上面的在w3c就不能正确用了,IE正确,但ff会显示不出来因为它不认识body.scrollHeight

它只认识documentElement.scrollHeight;

所以改成

< i frame  id=rightframe name=rightframe src="admin_right.aspx" marginWidth="10" frameSpacing="0" marginHeight="0" frameBorder="0" scrolling="no" align="center" width=785 height="1" frameborder="no" onload='this.height="500px";this.height=rightframe.document.documentElement.scrollHeight;'   ></ i frame>