Javascript Iframe属性onload被多次调用

Javascript Iframe属性onload被多次调用,javascript,html,iframe,Javascript,Html,Iframe,我有一个iframe,每次加载时都需要重新调整它的高度,我尝试了很多例子,但它们都在IE和Chrome上运行良好,但在FireFox上运行不好,直到我尝试了这一个,它可以在所有东西上运行 function autoResize() { var subscriptionFrame = jQuery("#frame_name_here"); var innerDoc = (subscriptionFrame.get(0).contentDocument

我有一个iframe,每次加载时都需要重新调整它的高度,我尝试了很多例子,但它们都在IE和Chrome上运行良好,但在FireFox上运行不好,直到我尝试了这一个,它可以在所有东西上运行

function autoResize() {
            var subscriptionFrame = jQuery("#frame_name_here");
            var innerDoc = (subscriptionFrame.get(0).contentDocument) ? subscriptionFrame
                    .get(0).contentDocument
                    : subscriptionFrame.get(0).contentWindow.document;
            if (innerDoc.body.scrollHeight == 0)
                innerDoc.location.reload(true);
            subscriptionFrame.height(innerDoc.body.scrollHeight + 10);
        }

<iframe src="uploadFile.xhtml" class="upload-iframe"
            id="frame_name_here" onload="autoResize();"></iframe>
函数自动调整大小(){
var subscriptionFrame=jQuery(“#frame_name_here”);
var innerDoc=(subscriptionFrame.get(0.contentDocument)?subscriptionFrame
.get(0).contentDocument
:subscriptionFrame.get(0).contentWindow.document;
if(innerDoc.body.scrollHeight==0)
innerDoc.location.reload(true);
subscriptionFrame.height(innerDoc.body.scrollHeight+10);
}

但是有一个问题,这个脚本多次调用onload(递归),知道为什么吗

我找到了一个答案,iframe部分显示为none,这并不意味着它不在Dom树中,这就是递归部分发生的原因,因为在本例中,iframe在显示iframe并通过resize函数读取高度内容之前没有获得任何高度。因此要解决这个问题,我使iframe部分在特定操作之前不会在dom树中呈现,而不是
display:none
,因此通过此操作,resize方法将只读取一次内容。

因为更改高度会触发加载,是因为这样:
innerDoc.location.reload(true)?我想如果(innerDoc.body.scrollHeight==0)innerDoc.location.reload(true),我发现了问题
此部分使其重新加载,但在某个操作之前,iframe高度为0,但此递归调用会向服务器发出大量调用。是否知道如何停止此操作?Remove
innerDoc.location.reload(true)?首先,您为什么要这样做?您需要重新加载iframe以获取内容的新高度