Javascript 基于窗口大小(而非内容)动态定义iframe高度

Javascript 基于窗口大小(而非内容)动态定义iframe高度,javascript,iframe,Javascript,Iframe,我发现了一些线程,比如说根据远程内容调整iframe的大小;但是,我还没有找到(在Firefox中)基于窗口大小设置iframe高度的方法 其他浏览器可以使用.body.scrollHeight,但Firefox似乎无法使用。看 要查看iframe的自动调整大小功能,请查看此页面 它适用于Chrome等浏览器,但不适用于Firefox。我不知道您是否想使用jQuery,但使用jQuery我想我已经解决了您的问题 // Header is 50px and footer is 50px; ther

我发现了一些线程,比如说根据远程内容调整iframe的大小;但是,我还没有找到(在Firefox中)基于窗口大小设置iframe高度的方法

其他浏览器可以使用
.body.scrollHeight
,但Firefox似乎无法使用。看

要查看iframe的自动调整大小功能,请查看此页面


它适用于Chrome等浏览器,但不适用于Firefox。我不知道您是否想使用jQuery,但使用jQuery我想我已经解决了您的问题

// Header is 50px and footer is 50px; therefore, 100px is of screen height is used
// Define content_height and consider the 100px which has already been used
var content_height=document.body.scrollHeight-100;
//alert(content_height);
content_height = $(window).height() -110;
//alert(content_height);
// Set iframe height
document.getElementById('pdf_frame').style.height=content_height+"px";

// Reset iframe height after window resize
$(function(){
    $(window).resize(function(){
        content_height = $(window).height()-110;
        //var content_height=document.body.scrollHeight-100;

        document.getElementById('pdf_frame').style.height=content_height+"px";
    });
});

以下是我做的跨浏览器修复

我添加了这个javascript函数

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}
然后我改变了

var content_height=document.body.scrollHeight-100;


你可以在

上看到它的实际作用。答案来自:所以这很好用。但是,如果动态删除元素,此方法将继续报告旧高度。想法?我知道这是一个古老的线索,但也看看这可能是另一种处理方法。
var content_height=getDocHeight()-100;