网上搜集的
原来写了很复杂的脚本,先获取页面参数值,再获取iframe高度,并在多个时间用setTimeout("autoResize()",10000)赋值给iframe现在利用样式表的功能就能轻易实现<SCRIPT>function autoResize(){try{ document.all["test"].style.height=test.document.body.scrollHeight}catch(e){}}</SCRIPT><IFRAME ID="test" SRC="ActorPictureList.aspx?ActorID=9371" FRAMEBORDER="0" STYLE="height:200; aho:expression(autoResize())"></IFRAME>
下面的代码在IE和FireFox中测试通过:
<iframe name="frame1" src="..." "this.height=window.frames[this.name].document.body.scrollHeight;">
跳转的话只能在跳转到的页内
<Body "parent.document.all.父页的iframe.height=this.document.body.scrollHeight;">
下面这段一定行
iframe 自适应高度 js实现
代码如下:
注意!不能跨域使用,传到服务器,或者自己的iis里运行iframe 本域页面.~~
引用
<iframe src="http://www.k900.cn /" style="width: 100% ; border: 0px" frameborder=0 scrolling=yes name=right "init()" border="0"></iframe>
<script>
var lastHeight;
function init() {
aaa();
lastHeight= right.document.body.scrollHeight;
setInterval(function(){
if(right.document.body.scrollHeight!=lastHeight) {
aaa();
lastHeight= right.document.body.scrollHeight;
}
},1);
}
function aaa(){
document.all('right').height=right.document.body.scrollHeight + 20;
}
</script>
<script>
function init(){
document.getElementById("oMain").height=window.frames["oMain"].document.body.scrollHeight;
}
</script>
<iframe "init()" id="oMain" name="oMain" width="100%" height="1000" src="<?=$url?>" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>
JavaScript应用:Iframe自适应其加载的内容高度
main.htm:
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=gb2312' />
<meta name='author' content='F.R.Huang(meizz梅花雪)//www.meizz.com' />
<title>iframe自适应加载的页面高度</title>
</head>
<body>
<div><iframe src="child.htm"></iframe></div>
</body>
</html>
child.htm:
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=gb2312' />
<meta name='author' content='F.R.Huang(meizz梅花雪)//www.meizz.com' />
<title>iframe 自适应其加载的网页(多浏览器兼容)</title>
<script type="text/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 h1=0, h2=0;
a[i].parentNode.style.height = a[i].offsetHeight +"px";
a[i].style.height = "10px";
if(document.documentElement&&document.documentElement.scrollHeight)
{
h1=document.documentElement.scrollHeight;
}
if(document.body) h2=document.body.scrollHeight;
var h=Math.max(h1, h2);
if(document.all) {h += 4;}
if(window.opera) {h += 1;}
a[i].style.height = a[i].parentNode.style.height = h +"px";
}
}
}
}
catch (ex){}
}
if(window.attachEvent)
{
window.attachEvent("onload", iframeAutoFit);
//window.attachEvent("onresize", iframeAutoFit);
}
else if(window.addEventListener)
{
window.addEventListener('load', iframeAutoFit, false);
//window.addEventListener('resize', iframeAutoFit, false);
}
//-->
</script>
</head>
<body>
<table border="1" width="200" style="height: 400px; background-color: yellow">
<tr>
<td>iframe 自适应其加载的网页(多浏览器兼容,支持XHTML)</td>
</tr>
</table>
</body>
</html>
很多人反应在IE7里使用它会死机,那是因为在自适应高度时触发了 window.onresize 事件,而这个事件又去调用这个调整 <iframe> 高度的函数,产生了死循环调用。
<A href="http://pc372.bokee.com/pub/neweditor/editor/>
你可以通过这个链接引用该篇文章:http://pc372.bokee.com/viewdiary.13585798.html