js Iframe 自动适应 高度 宽度

来源:互联网 发布:电子书阅读软件哪个好 编辑:程序博客网 时间:2024/05/17 08:24
 

/* File Created: 十一月 25, 2011 */

//使用方法 在iframe的onload事件中加载
//<iframe onload="reinitIframe(this)" id="frame_content" src="Default2.aspx" width="100" frameborder="0" height="100%"
//            scrolling="no"></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;
        }
    }
}

//iframe 高度自适应
function reinitIframe(iframeid) {
    var iframe = document.getElementById(iframeid);
    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.setInterval("reinitIframe()", 200);

原创粉丝点击