Javascript 使用chrome自动调整iframe的大小

Javascript 使用chrome自动调整iframe的大小,javascript,jquery,html,Javascript,Jquery,Html,我被要求根据iFrame的内容调整iFrame的大小。经过长时间的搜索,我终于让它可以根据需要工作,但它只在firefox上工作。我需要它至少在chrome、ff和ie上工作。这是脚本 <script language="JavaScript"> <!-- function autoResize(id){ var newheight; if(document.getElementById){ newheight=document.getElem

我被要求根据iFrame的内容调整iFrame的大小。经过长时间的搜索,我终于让它可以根据需要工作,但它只在firefox上工作。我需要它至少在chrome、ff和ie上工作。这是脚本

<script language="JavaScript">
<!--
function autoResize(id){
    var newheight;

    if(document.getElementById){
        newheight=document.getElementById(id).contentWindow.document.body.scrollHeight; //ff
        newheight=document.getElementById(id).contentDocument.body.offsetHeight; //chrome
        newheight=document.getElementById(id).contentDocument.documentElement.scrollHeight;

        }

    document.getElementById(id).height= (newheight) + "px";
}
//-->
</script>

和iframe代码

<iframe name="result" id="iframe" seamless src="action/search.php" width="100%" onLoad="autoResize('iframe');" marginwidth=0 marginheight=0 frameborder=0 scrolling="no" style="border:0; overflow:hidden">
    </iframe>

欢迎提出任何建议。

试试这个

onload="if (window.parent &amp;&amp; window.parent.autoIframe) {window.parent.autoIframe('tree');}
在iframe和

<script type="text/javascript">
    function autoIframe(frameId){
        try{
            frame = document.getElementById(frameId);
            innerDoc = (frame.contentDocument) ? frame.contentDocument : frame.contentWindow.document;

            if (innerDoc == null){
                            // Google Chrome
                frame.height = document.all[frameId].clientHeight + document.all[frameId].offsetHeight + document.all[frameId].offsetTop;
            }
                    else{
                    objToResize = (frame.style) ? frame.style : frame;
                    objToResize.height = innerDoc.body.scrollHeight + 18;
                    }
        }

        catch(err){
                alert('Err: ' + err.message);
            window.status = err.message;
        }
    }
    </script>

函数autoIframe(frameId){
试一试{
frame=document.getElementById(frameId);
innerDoc=(frame.contentDocument)?frame.contentDocument:frame.contentWindow.document;
if(innerDoc==null){
//谷歌浏览器
frame.height=document.all[frameId].clientHeight+document.all[frameId].offsetHeight+document.all[frameId].offsetTop;
}
否则{
objToResize=(frame.style)?frame.style:frame;
objToResize.height=innerDoc.body.scrollHeight+18;
}
}
捕捉(错误){
警报(“错误:”+错误消息);
window.status=err.message;
}
}

与其直接设置元素的高度,不如将其设置为属性:

var heightAttr = document.createAttribute("height");
heightAttr.value = newheight;
document.getElementById(id).attributes.setNamedItem(heightAttr);
或者您可以在css对象中设置它:

document.getElementById(id).style.height = (newheight) + "px";

检查这个答案听起来像是解决了同样的问题。我已经尝试过了,但每次我点击提交按钮时它都会打开一个新的标签。嗨,谢谢你的帮助。我收到一个错误,在ff中说Err:frame为null,在Chrome中说“Err:cannotreadproperty'contentDocument'of null”。嗨,我实际上对javascript不是很有经验。我实际上还在努力学习基础知识,也许你能帮我,给我更多关于如何做到这一点的细节?谢谢你的帮助