js代码保证iframe适应被加载页面的高度

来源:互联网 发布:淘宝数据修改软件 编辑:程序博客网 时间:2024/05/29 19:21


经常使用iframe加载一些页面,这些页面高度各不相同,如何使用js确保加载这些网页时,iframe自动适应其高度?代码如下,关键是这个IFrameReSize('iframepage') 函数

 

<iframename="mainFrame"  width="99%"   src="use_zhifubao.aspx"   id="iframepage"frameborder="0" scrolling="no" marginheight="0"marginwidth="0"onload="IFrameReSize('iframepage')"></iframe>

 

//iframe高度自适应

 

    function IFrameReSize(iframename) {

        var pTar = document.getElementById(iframename);

        if (pTar) { //f

            if (pTar.contentDocument &&pTar.contentDocument.body.offsetHeight) {

                pTar.height =pTar.contentDocument.body.offsetHeight;

            } //ie

            else if (pTar.Document && pTar.Document.body.scrollHeight){

                pTar.height =pTar.Document.body.scrollHeight;

            }

        }

}

此代码在360浏览器 8.1的极速模式和IE模式下测试成功

0 0