方法:在主页面Iframe的onload事件中执行JS,去取得被包含页的高度,然后去同步高度。并在主窗口做一个Interval,不停的来获取被包含页的高度。当Iframe窗体高度高于文档实际高度的时候,高度取的是窗体高度,而当窗体高度低于实际文档高度时,取的是文档实际高度。因此,要想办法在同步高度之前把高度设置到一个比实际文档低的值。所以,添加 οnlοad=”this.height=100″,让页面加载的时候先缩到足够矮,然后再同步到一样的高度。
代码如下所示:
<Iframe src="index.html" name="contain" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" id="contain" οnlοad="this.height=100" style=" WIDTH: 100%; "></Iframe>
// height=100这个数值可以根据需要自行设置,即页面加载时显示的最小高度。
<script type="text/javascript">
function reinitIframe(){
var Iframe = document.getElementById("contain");
try{
// 声明变量取值
var bHeight = Iframe.contentWindow.document.body.scrollHeight;
vardHeight=Iframe.contentWindow.document.documentElement.scrollHeight;
var height = Math.max(bHeight, dHeight); // 取最大值
Iframe.height = height;
}catch (ex){}
}
window.setInterval("reinitIframe()", 200);
</script>