main.htm 主页面

<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=gb2312' />
<title>iframe自适应加载的页面高度</title>
</head>
<body>
<iframe src="child.htm"></iframe>
</body>
</html>


frame.htm 内嵌框架页

<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=gb2312' />
<title>iframe 自适应其加载的网页(多浏览器兼容)</title>

<script type="text/javascript">
<!--
function iframeAutoFit()

{
var ex;
try

{
if(window!=parent)

{
var a = parent.document.getElementsByTagName("IFRAME");
for(var i=0; i<a.length; i++) //author:meizz

{
if(a[i].contentWindow==window)

{
var h1=0, h2=0, h3=0, h4=0;
if(document.documentElement&&document.documentElement.scrollHeight)

{
h1 = document.documentElement.scrollHeight;
h3 = document.documentElement.scrollWidth;
}
if(document.body) h2=document.body.scrollHeight;h4=document.body.scrollWidth;

var h=Math.max(h1, h2);
var h0=Math.max(h3,h4);
a[i].style.height = h +"px";
a[i].style.width = h0 + "px";
}
}
}
}

catch (ex)
{}
}
if(document.attachEvent)

{
window.attachEvent("onload", iframeAutoFit);
window.attachEvent("onresize", iframeAutoFit);
}
else

{
window.addEventListener('load', iframeAutoFit, false);
window.addEventListener('resize', iframeAutoFit, false);
}
//-->
</script>
</head>
<body style="overflow:hidden;border:0px">
<table border="1" width="200" style="height: 200px;width:300px;">
<tr>
<td>iframe 自适应其加载的网页(多浏览器兼容,支持XHTML)</td>
</tr>
</table>
</body>
</html>

转载于:https://www.cnblogs.com/hahacjh/archive/2010/05/16/1736575.html