iframe 自适应高度

<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了。

 
原文地址:https://www.cnblogs.com/zhonghan/p/4527623.html