iframe的自适应高度

<iframe src="index.html" id="iframepage" name="iframepage" frameBorder=0 scrolling=no width="100%" onLoad="reinitIframeEND();">
</iframe>
<script type="text/javascript" language="javascript">

function reinitIframe(){
var iframe = document.getElementById("iframepage");
try{
var bHeight = iframe.contentWindow.document.body.scrollHeight;
var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
var height = Math.max(bHeight, dHeight);
iframe.height = height;
}catch (ex){}
}

var timer1 = window.setInterval("reinitIframe()", 500); //定时开始

function reinitIframeEND(){
var iframe = document.getElementById("iframepage");
try{
var bHeight = iframe.contentWindow.document.body.scrollHeight;
var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
var height = Math.max(bHeight, dHeight);
iframe.height = height;
}catch (ex){}
// 停止定时
window.clearInterval(timer1);

}

posted @ 2016-05-23 15:10  vagebird  阅读(145)  评论(0编辑  收藏  举报