6

I have a call like this:

$("#ContextMenuModal").height($("body").height());

However $("body").height() returns undefined when I view it in Firebug.

What would cause jQuery to return undefined for the body height?

I am using jQuery 1.4.1.

Edit:

This is inside an iFrame

dmck
  • 7,801
  • 7
  • 43
  • 79
  • Without seeing a test case, we can only have wild assumptions about what can cause this. Please create a demo or show the page. Here is a [demo that shows it working](http://jsfiddle.net/BBf3g/). Your turn. – kapa Jun 13 '12 at 21:04
  • @bažmegakapa It is a large application I am unable to post, I can attempt to create a repro, however, I was wondering if there were cases where jquery cannot determine the height. – dmck Jun 13 '12 at 21:11
  • It's always difficult (mostly impossible?) to find a bug when you cannot see the code and the program running. – kapa Jun 13 '12 at 21:14
  • 2
    Are you running this after the page has loaded? – epascarello Jun 13 '12 at 21:28

2 Answers2

14

Simply use

$(document).height() // - $('body').offset().top

and / or

$(window).height()

instead $('body').height()

To expand a bit,

$(window).height();   // returns height of browser viewport
$(document).height(); // returns height of HTML document

As bažmegakapa points out, there is a slight difference, albeit a few pixels. The true height of the body can be calculated by subtracting the body offset from the document height (like I mentioned above):

$(document).height() - $('body').offset().top
Norse
  • 5,674
  • 16
  • 50
  • 86
  • 1
    -1 for showing the functions and not providing an explanation/description. There is a big difference between these two (well, three). – kapa Jun 13 '12 at 21:03
  • 1
    $(window).height() and $(document).height() are also both returning undefined for me – dmck Jun 13 '12 at 21:07
  • 1
    @Norse Few pixels, yeah. The document can be much higher than the viewport. And if you set `body { margin-top: 2000px; }`, I'd say the document will be much bigger than the body :). [A previous question about this](http://stackoverflow.com/questions/9431050). – kapa Jun 13 '12 at 21:11
0

Use this-

 $("body").css("height")

well i will say jquery is not needed-

this.style.height;//it will not work if you add it as inline js.
Ash
  • 3,431
  • 2
  • 15
  • 15