0

I am trying to get total height of webpage using javascript as follows

var pageHeight = (document.height !== undefined) ? document.height : document.body.offsetHeight;

works fine for me in other browsers but Internet explorer returns a value '0' for it. why?

Abhishek Umrao
  • 171
  • 1
  • 4
  • 11
  • 1
    Which version IE? Their versions are as different as black and white.. – techfoobar May 05 '13 at 12:07
  • @techfoobar IE 10. the latest one! – Abhishek Umrao May 05 '13 at 12:10
  • Your code works fine in IE9 and IE10 both, see [this fiddle](http://jsfiddle.net/9gpht/1/). I think the reason for your confusion is that all browsers have a different idea of what the `offsetHeight` is - it's not identical to the viewport, and I think you're looking for that. – Niels Keurentjes May 05 '13 at 12:11
  • if you're in IE10, have you checked that your page is actually in IE10 standards mode, and not compatibility mode or quirks mode. IE10 itself should be fine, but these older modes will definitely have issues like the one described. – Spudley May 05 '13 at 13:56

1 Answers1

2

This should work in all browsers :

var pageHeight = Math.max(document.height, document.body.scrollHeight,
    document.body.offsetHeight);

Don't forget to execute the code after the document is loaded.

EDIT : I let this in hope it works but I have no way to test it in all browsers and I'm not 100% sure. It's adapted from jQuery's source.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758