博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

iframe 自适应高度

Posted on 2015-05-25 13:10  钟悍  阅读(119)  评论(0编辑  收藏  举报
<iframe id="iframePage" name="iframePage" src="http:www.xxx.com" scrolling="no" height="100%" width="1300" onload="Javascript:setWinHeight()" ></iframe>
<iframe id="iframePage" name="iframePage" src="http:www.xxx.com" scrolling="no" height="100%" width="1300" onload="Javascript:setWinHeight()" ></iframe>
function setWinHeight() {
    setInterval(setIframeHeight, 2000);
}

function setIframeHeight(){
    var ifm= document.getElementById("iframePage");
    var subWeb = document.frames ? document.frames["iframePage"].document : ifm.contentDocument;
    if(ifm != null && subWeb != null) {
        ifm.height = subWeb.body.scrollHeight;
    }
}

 

如果iframe 的页面是通过ajax动态加载的,那么需要通过 setInterval来修改iframe的高度,

如果iframe的页面是静态的页面, 则onload方法直接调用setIframeHeight方法就OK了。