天天看点

iframe.js

/**
*createTime:2016-04-26
*author:李志伟
*Description: iframe高度自适应
**/

//使用例子
//<a href="Information.html" target="mainFrame">在mainFrame框架打开页面</a>  
//<a href="about.html" target="mainFrame">在mainFrame框架打开页面</a>  
//<iframe id="mainFrame" name="mainFrame" src="Information.html" 
//scrolling="no" frameborder="0" height="100%" width="100%" 
//οnlοad='IFrameReSize("mainFrame")'></iframe>


//iframe高度自适应
function IFrameReSize(iframename) {
    var pTar = document.getElementById(iframename);
    if (pTar) {  //ff
        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;
        }
    }
}

//iframe宽度自适应
function IFrameReSizeWidth(iframename) {
    var pTar = document.getElementById(iframename);
    if (pTar) {  //ff
        if (pTar.contentDocument && pTar.contentDocument.body.offsetWidth) {
            pTar.width = pTar.contentDocument.body.offsetWidth;
        }  //ie
        else if (pTar.Document && pTar.Document.body.scrollWidth) {
            pTar.width = pTar.Document.body.scrollWidth;
        }
    }
}