Iframe自适应其加载的内容高度
程序员文章站
2024-02-29 08:21:04
...
<html>
<head>
<meta http-equiv='content-type' content='text/html; charset=gb2312'>
<meta name='author' content='站长中国 http://www.zzcn.net/'>
<title>iframe自适应加载的页面高度</title>
</head>
<body>
<iframe src="child.htm"></iframe>
</body>
</html>
child.htm:
<html>
<head>
<meta http-equiv='content-type' content='text/html; charset=gb2312'>
<meta name='author' content='站长中国 http://www.zzcn.net/'>
<title>iframe 自适应其加载的网页(多浏览器兼容)</title>
<script language=javascript>
function iframeautofit()
{
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 h = document.body.scrollheight;
if(document.all) {h += 4;}
if(window.opera) {h += 1;}
a[i].style.height = h;
}
}
}
}
catch (ex)
{
alert("脚本无法跨域操作!");
}
}
if(document.attachevent) window.attachevent("onload", iframeautofit);
else window.addeventlistener('load', iframeautofit, false);
</script>
</head>
<body>
<div style="width: 200; height: 400; background-color: yellow">
iframe 自适应其加载的网页(多浏览器兼容)
</div>
</body>
</html>
<head>
<meta http-equiv='content-type' content='text/html; charset=gb2312'>
<meta name='author' content='站长中国 http://www.zzcn.net/'>
<title>iframe自适应加载的页面高度</title>
</head>
<body>
<iframe src="child.htm"></iframe>
</body>
</html>
child.htm:
<html>
<head>
<meta http-equiv='content-type' content='text/html; charset=gb2312'>
<meta name='author' content='站长中国 http://www.zzcn.net/'>
<title>iframe 自适应其加载的网页(多浏览器兼容)</title>
<script language=javascript>
function iframeautofit()
{
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 h = document.body.scrollheight;
if(document.all) {h += 4;}
if(window.opera) {h += 1;}
a[i].style.height = h;
}
}
}
}
catch (ex)
{
alert("脚本无法跨域操作!");
}
}
if(document.attachevent) window.attachevent("onload", iframeautofit);
else window.addeventlistener('load', iframeautofit, false);
</script>
</head>
<body>
<div style="width: 200; height: 400; background-color: yellow">
iframe 自适应其加载的网页(多浏览器兼容)
</div>
</body>
</html>