日期:2014-05-20  浏览次数:20684 次

iframe自适应高度
<script type="text/javascript">

function reinitIframe(){

var iframe = document.getElementById("reinitIframe");

try{

var bHeight = iframe.contentWindow.document.body.scrollHeight;

var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;

var height = Math.max(bHeight, dHeight);
iframe.height = height;
iframe.width=1001;
//alert(iframe.height);
//document.getElementById('TabContent').style.height=height;

}catch (ex){}

}

window.setInterval("reinitIframe()", 218);

</script> 

<iframe id="reinitIframe" name="ProductViewIframe" src="<%=DefaultSelected %>" style="width:1003px;" scrolling="no" frameborder="0" onload="this.height=218"></iframe>
我使用了上面的代码实现iframe的自适应高度,实现是实现了,但是,滚动条一直在走,页面的高度一直在增加,很不友好,我该怎么让滚动条不走呢?


------解决方案--------------------
// somewhere in your js file:
function resize(id)
{
var height = document.getElementById(id).contentWindow.document.body.scrollHeight;
var width = document.getElementById(id).contentWindow.document.body.scrollWidth;

document.getElementById(id).height= (height) + "px";
document.getElementById(id).width= (width) + "px";
}

<iframe id="content" name="content" src="section.htm" scrolling="no" frameborder="0" border="0" onLoad="resize('content')"></iframe>

=====

这个是我以前用过的一段重设iframe大小的代码,去掉重设宽度的部分就行了,希望对你有帮助。
对比一下,你用的那段代码中的这一句:
var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
可能取得了某父元素的高度,结合max那句造成了无限循环。