8

What is the best way to get the actual page (not window) height in JS that is cross-browser compatible?

I've seen a few ways but they all return different values...

self.innerHeight or document.documentElement.clientHeight or document.body.clientHeight or something else?

One way of doing it which seems to work is :

var body = document.body,
    html = document.documentElement;

var height = Math.max( body.scrollHeight, body.offsetHeight, 
                       html.clientHeight, html.scrollHeight, html.offsetHeight );
Boris Bachovski
  • 642
  • 2
  • 12
  • 22

3 Answers3

4

Page/Document height is currently subject to vendor (IE/Moz/Apple/...) implementation and does not have a standard and consistent result cross-browser.

Looking at JQuery .height() method;

if ( jQuery.isWindow( elem ) ) {
            // Everyone else use document.documentElement or document.body depending on Quirks vs Standards mode
            // 3rd condition allows Nokia support, as it supports the docElem prop but not CSS1Compat
            var docElemProp = elem.document.documentElement[ "client" + name ],
                body = elem.document.body;
            return elem.document.compatMode === "CSS1Compat" && docElemProp ||
                body && body[ "client" + name ] || docElemProp;

        // Get document width or height
        } else if ( elem.nodeType === 9 ) {
            // Either scroll[Width/Height] or offset[Width/Height], whichever is greater
            return Math.max(
                elem.documentElement["client" + name],
                elem.body["scroll" + name], elem.documentElement["scroll" + name],
                elem.body["offset" + name], elem.documentElement["offset" + name]
            );

nodeType === 9 mean DOCUMENT_NODE : http://www.javascriptkit.com/domref/nodetype.shtml so no JQuery code solution should looks like:

var height = Math.max(
                    elem.documentElement.clientHeight,
                    elem.body.scrollHeight, elem.documentElement.scrollHeight,
                    elem.body.offsetHeight, elem.documentElement.offsetHeight)
Krilivye
  • 547
  • 4
  • 8
1
var width = window.innerWidth ||
            html.clientWidth  ||
            body.clientWidth  ||
            screen.availWidth;

var height = window.innerHeight ||
             html.clientHeight  ||
             body.clientHeight  ||
             screen.availHeight;

Should be a nice & clean way to accomplish it.

Jonas
  • 44
  • 3
0

Try this without jQuery

//Get height
var myWidth = 0, myHeight = 0;
if (typeof (window.innerWidth) == 'number') {
    myWidth = window.innerWidth; 
    myHeight = window.innerHeight;
} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
    myWidth = document.documentElement.clientWidth; 
    myHeight = document.documentElement.clientHeight;
} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
   myWidth = document.body.clientWidth; 
   myHeight = document.body.clientHeight;
}

Hope this helps you.

Amar Palsapure
  • 9,590
  • 1
  • 27
  • 46
  • Tried that just now in firebug on this particular page returns 456px height which is incorrect... – Boris Bachovski Feb 24 '12 at 11:44
  • @e-bacho2.0 Try again, and pop out your firebug window. I'm getting same value as `$(window).height();` <= this is jQuery (and which is correct) `705` in both the cases. – Amar Palsapure Feb 24 '12 at 11:48