在IE上获取iframe中的身高

时间:2014-02-28 16:10:30

标签: javascript

在iframe中我试图与父文档通信内容高度以调整iframe的大小。通信工作完美,问题在于获取内容高度。即使在加载DOM之后,给定的高度是默认值30 px而不是实际高度,如5000px。我只在IE 8和IE 9上遇到这个问题。

我曾尝试使用window.onload事件,但它没有被调用,所以我尝试了一个循环,每秒获取内容高度,以查看它是否得到更新,但即使在所有DOM之后已加载,问题仍然存在。

我有这样的事情:

checkHeightLoop = setInterval(function(){resizeIframe()}, 1000);

function resizeIframe(){
  parent.postMessage("resizeIframe#" + $('body').outerHeight( true ), "*");
}

循环是愚蠢的,但只是看看我是否可以在加载DOM后得到不同的高度,因为事件window.onload没有被调用。除了获取内容高度的Jquery方法,我已经尝试过了:

var x = window.innerHeight,
    y = window.scrollY,
    z = document.body.clientHeight,
    a = document.body.scrollHeight,
    b = document.body.offsetHeight,
    c = document.documentElement.clientHeight,
    d = document.documentElement.scrollHeight,
    e = document.documentElement.offsetHeight,

1 个答案:

答案 0 :(得分:0)

我需要查看整个代码才能获得图片,但是......

您是否尝试过使用"window.onload" javascript event handler。它在DOM之后激活。

好的,在阅读了更多有关您的问题之后。在您的父文档中尝试此代码:

<script language="JavaScript">
function aResize(id)
{
    var nh;
    var nw;
        nh=document.getElementById(id).contentWindow.document .body.scrollHeight;
        nw=document.getElementById(id).contentWindow.document .body.scrollWidth;
        document.getElementById(id).height= (nh) + "px";
        document.getElementById(id).width= (nw) + "px";
}
</script>

<iframe src="yourpage.aspx" width="100%" height="100px" id="if1" frameborder="0" onLoad="aResize('if1');"></iframe>