2014-02-28 22 views
0

在一個iframe中,我試圖與父文檔通信的內容高度,以調整iframe的大小。溝通工作完美,問題在於獲取內容高度。即使在DOM被加載後,給定的高度是默認的30px,而實際的高度也是5000px。我只在IE 8和IE 9上遇到這個問題。獲取iframe中的身體高度在IE

我試圖使用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, 

回答

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> 
+0

感謝您的回答,我編輯我的帖子的代碼。 – user3365583

+0

試試上面的答案。 – Rich

+0

謝謝,由於父母在另一個域中,我將能夠做document.getElementById(id).contentWindow.document .body.scrollHeight? – user3365583