iframe自适应宽高

来源:互联网 发布:数据质量自查报告 编辑:程序博客网 时间:2024/04/25 23:27

在div 中嵌了一个iframe,iframe中的内容高度不定。开始找了个自适应高度的,IE6过了,IE8中iframe显示的很小,IE7对iframe中的内容完全不显示。试了半天终于找到一个能用的,让宽度和高度都自适应,虽然在MyEclipse里面一直用个小黄图标提示iframe没有onload属性,不过还是能起作用的。用IETester测试了一下,IE5678都可以的。

function reSetIframe(){

    var iframe = document.getElementById("mainFrame");

    try{

        var bHeight = iframe.contentWindow.document.body.scrollHeight;

        var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;


        var bWidth= iframe.contentWindow.document.body.scrollWidth;

        var dWidth=iframe.contentWindow.document.documentElement.scrollWidth;

        var height = Math.max(bHeight, dHeight);

        var width= Math.max(bWidth, dWidth);

        iframe.height =  height;

        iframe.width=width;

    }catch (ex){}

}


<iframe name='mainFrame' id='mainFrame' onload="reSetIframe()" frameborder="0" scrolling="no"  src="allProduct.action"></iframe>

原创粉丝点击