2012-09-12 2 views
1

콘텐츠를 기준으로 iFrame의 크기를 조정하는 것만 큼 문제가 있습니다. 나는 다른 곳에서 일하고 있지만이 한 예에서는 오류가 발생합니다. 다음 코드를 사용하면 body이 null이므로 scrollHeight을 얻을 수 없습니다.contentWindow.document.body is null

if(document.getElementById('iFrameHelpDesk') != null) 
{ 
    var Height = document.getElementById('iFrameHelpDesk').contentWindow.document.body.scrollHeight; 

    document.getElementById('iFrameHelpDesk').height = Height + 40; 
} 

사용되는 HTML은 다음과 같습니다

<iframe src="http://<snipped webaddress>" id="iFrameHelpDesk" scrolling="no" frameborder="no"></iframe> 

가 왜이 body 객체의 이식하지 않는 이유는 무엇입니까?

+1

iframe의 기본 페이지와 다른 도메인이 있으면 보안상의 이유로 iframe에 액세스 할 수 없습니다. – grc

+0

@ grc - 페이지가 동일한 도메인에 있으므로이 문제가 없습니다. – anothershrubery

+0

위의 요점 외에도, 위의 코드는 내가 지정한 다른 모든 iframe에도 적용됩니다. 문제를 일으키는 것은 바로이 것입니다. – anothershrubery

답변

2

간단히 말해서 문서로드 ($(function() {})) 메소드에서 코드를 가져 와서 실제 iframe이로드 될 때 자신의 메소드에 넣습니다. 예.

$('#iFrameHelpDesk').load(function() { 

    var Height = document.getElementById('iFrameHelpDesk').contentWindow.document.body.scrollHeight; 

    document.getElementById(iFrameHelpDesk').height = Height + 40; 

}); 

제대로 작동합니다. 그러나 이제는 비슷한 코드를 수행하는 다른 iframe이 문서로드 내에서 제대로 작동하는 이유에 대해 혼란 스럽습니다. 그러나이 작품과 나는 충분히 행복합니다.