views:

558

answers:

5

I'm trying to get the total height of a page using JavaScript and jQuery so I can check if the page is long enough to display something, however in my testing I am unable to get the total height of a page.

I've looked around on the Internet but things like this don't seem to be well documented, as all I can find is scrollHeight, which, as I might mention, doesn't work.

Any way to use jQuery to find it?

A: 

have you tried $(document).height();

Demo here

redsquare
That gives an error.
Brandon Wang
works for me http://pastebin.me/4a52303ca45de - make sure you have the firefox+firebug console open
redsquare
A: 

Have a look at the documentation. One of the supported methods: height, innerHeight, outerHeight may be suitable for you.

kgiannakakis
A: 

perhaps use the position of an element at the bottom of the page, like:

$("#footer").offset().top

mupdyke
+1  A: 

Without a framework:

var _docHeight = (document.height !== undefined) ? document.height : document.body.offsetHeight;
var _docWidth = (document.width !== undefined) ? document.width : document.body.offsetWidth;
Andreas Grech
A: 

Height of entire page...

document.body.offsetHeight

Height of viewport...

var h;

if(self.innerHeight)
  h = window.innerHeight
else if(document.documentElement && document.documentElement.clientHeight)
  h = document.documentElement.clientHeight;
else if(document.body)
  h= document.body.clientHeight;

This article may help you.

Josh Stodola