Javascript的iframes调整功能不工作在谷歌浏览器

Javascript resize function for iframes does not work in google chrome

本文关键字:工作 谷歌浏览器 功能 iframes 调整 Javascript      更新时间:2023-09-26

我使用了一个在stackoverflow上找到的javascript调整大小函数,它在IE和FF上工作得很好。然而,在谷歌浏览器中,它只能调整比前一个更大的iframe。如果它需要调整较小的iframe,它取相同的高度并添加30px。

我认为它与检索scrollHeight有关,但我不知道如何修复它。

我已经尝试使用超时,因为我在某处读到可能有帮助,但它没有。

相关代码如下:

    function autoResize(id){
        var newheight;
        if(document.getElementById)
        {
            newheight=document.getElementById(id).contentWindow.document.body.scrollHeight;
            newheight=newheight+30;
        }
        document.getElementById(id).height = (newheight) + "px";
    }
    <iframe src="<?php echo "$LC"; ?>/home.html" name="iframe" id="iframe" width="435px" height="10px" scrolling="no" frameborder="0" style="margin-left:-10px;padding-bottom:20px;" onLoad="setTimeout(autoResize('iframe'),250);";">

编辑:通过将函数更改为

来解决
    function autoResize(id) {
        document.getElementById(id).height = document.getElementById(id).contentDocument.documentElement.scrollHeight+15; //Chrome
        document.getElementById(id).height = document.getElementById(id).contentWindow.document.body.scrollHeight+15; //FF, IE
    }
function autoResize(id) {
    document.getElementById(id).height = document.getElementById(id).contentDocument.documentElement.scrollHeight+15; //Chrome
    document.getElementById(id).height = document.getElementById(id).contentWindow.document.body.scrollHeight+15; //FF, IE
}