javascript - contentWindow.document.body is null - Stack Overflow

I have a problem in my javascript which is just to resize an iFrame based on the content. I have it wor

I have a problem in my javascript which is just to resize an iFrame based on the content. I have it working in other places but for this one example it is throwing an error. Using the following code the body is null and therefore can't get scrollHeight.

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

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

The html used is:

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

Why does this not populate the body object?

I have a problem in my javascript which is just to resize an iFrame based on the content. I have it working in other places but for this one example it is throwing an error. Using the following code the body is null and therefore can't get scrollHeight.

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

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

The html used is:

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

Why does this not populate the body object?

Share Improve this question asked Sep 12, 2012 at 10:05 anothershruberyanothershrubery 21k16 gold badges56 silver badges101 bronze badges 3
  • 1 If the iframe has a different domain to the main page, then you can't access it for security reasons. – grc Commented Sep 12, 2012 at 10:09
  • @grc - The page is on the same domain so does not have this problem. – anothershrubery Commented Sep 12, 2012 at 10:43
  • In addition to my point above, the above code also works for all other iframes that I specify. It's just this one that is causing an issue. – anothershrubery Commented Sep 12, 2012 at 11:07
Add a ment  | 

1 Answer 1

Reset to default 5

Fairly simply I took the code from the document load ($(function() {})) method and put it into it's own method for when the actual iframe loads. Ie.

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

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

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

});

This works perfectly, as it should. But it's now confused me as to why some other iframes, which I perform similar code on, are working fine from within the document load. But this works and I'm happy enough with that.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742368361a4430768.html

相关推荐

  • javascript - contentWindow.document.body is null - Stack Overflow

    I have a problem in my javascript which is just to resize an iFrame based on the content. I have it wor

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信