Here the total height of all <div>
's are 900 pixels, but the jQuery function returns the height of the body as 577 pixels. (If I remove the body CSS, it's working).
Is there a solution for this problem?
$j(function() {
alert($j("body").height());
})
html, body {
height:100%;
}
<div style="height:200px">header</div>
<div style="height:500px">content</div>
<div style="height:200px">footer</div>
I believe that the body height being returned is the visible height. If you need the total page height, you could wrap your div tags in a containing div and get the height of that.
Simply use
$(document).height() // - $('body').offset().top
and / or
$(window).height()
instead of $('body').height();
$(document).height() seems to do the trick and gives the total height including the area which is only visible through scrolling.
We were trying to avoid using the IE specific
$window[0].document.body.clientHeight
And found that the following jQuery will not consistently yield the same value but eventually does at some point in our page load scenario which worked for us and maintained cross-browser support:
$(document).height()
Source: Stackoverflow.com