Some documents I can't get the height of the document (to position something absolutely at the very bottom). Additionally, a padding-bottom on seems to do nothing on these pages, but do on the pages where height will return. Case(s) in point:
http://fandango.com
http://paperbackswap.com
On Fandango
jQuery's $(document).height();
returns correct value
document.height
returns 0
document.body.scrollHeight
returns 0
On Paperback Swap:
jQuery's $(document).height();
TypeError: $(document)
is null
document.height
returns an incorrect value
document.body.scrollHeight
returns an incorrect value
Note: I have browser level permissions, if there is some trick there.
The "jQuery method" of determining the document size - query everything, take the highest value, and hope for the best - works in most cases, but not in all of them .
If you really need bullet-proof results for the document size, I'd suggest you use my jQuery.documentSize plugin. Unlike the other methods, it actually tests and evaluates browser behaviour when it is loaded and, based on the result, queries the right property from there on out.
The impact of this one-time test on performance is minimal, and the plugin returns the right results in even the weirdest scenarios - not because I say so, but because a massive, auto-generated test suite actually verifies that it does.
Because the plugin is written in vanilla Javascript, you can use it without jQuery, too.
The proper answer for 2017 is:
document.documentElement.getBoundingClientRect().height
Unlike
document.body.scrollHeight
this method accounts for body margins. It also gives fractional height value, which can be useful in some casesuse blow code for compute height + scroll
Add References properly
In my case I was using a ASCX page and the aspx page that contains the ascx control is not using the references properly. I just pasted the following code and it worked :
You can even use this:
or in a more jQuery way (since as you said jQuery doesn't lie) :)
Get the width in a cross browser/device way use: