The following code should return the full height of the document, but it does not. It says the height is 1023
but (measuring by screenshots and scrolling and photoshop) the actual height is 1804
. Why is it wrong? How do I get the full height?
var height = Math.max(
document.body.scrollHeight,
document.body.offsetHeight,
document.documentElement.offsetHeight,
document.body.clientHeight,
document.documentElement.clientHeight
);
EDIT I was not clear. I want document height, not window height. By “screenshots and scrolling and photoshop”, I meant I did the following:
- Took a screenshot and copied the window height and put in a new image
- Scrolled a full window height again and took a new screenshot and pasted that onto #1
- Continued until I couldn’t scroll anymore and pasted in the remaining height
That gave me document height.
5
Answers
If you want your page to occupy the full height of the browser window, you can add this CSS rule:
That tells the browser that the body should be at least as big as the viewport. Things like that work best when you’ve imposed
border-box
box sizing on everything:That way padding and borders won’t cause unwanted unnecessary scroll bars.
What about window.innerHeight; ? That sounds like what you would be measuring in a screenshot of the browser window.
There is a difference between document and window height.
It looks like you are trying to get the window height.
Try using
window.innerHeight
instead or the document heights. 🙂I think you did a mistake while copy-pasting your screens.
I just made my own tests using following script to be sure I was scrolling one page height at a time :
Results show that your function give the same height as what copy-pasting each window does.
However, as goldfire pointed out, if you were zooming in your page, result might be different.
The
document
itself has no height. You can only get the height of thedocumentElement
, which is the representation of the document. All following approaches gives the same result:See working Fiddle here.
I have tried your method with a screenshot in the following way: I opend a document much higher than the screen in Firefox, catched the whole page with FireShot (its possible without scrolling) and saved it as an image. Its pixel-height was exactly the same as what I got with h0, h1, h2 above.