Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript - Get Browser Height

I am looking for a code snippet to get the height of the viewable area within a browser window.

I had this code, however it is somewhat bugged as if the the body doesn't exceed the height the of the window then it comes back short.

document.body.clientHeight; 

I have tried a couple of other things but they either return NaN or the same height as the above.

Does anyone know how to get the real height of the browsing window?

like image 971
JasonS Avatar asked Jul 26 '10 08:07

JasonS


People also ask

How do I find my browser width and height?

Use window. innerWidth and window. innerHeight to get the current screen size of a page.

What is innerHeight in Javascript?

The read-only innerHeight property of the Window interface returns the interior height of the window in pixels, including the height of the horizontal scroll bar, if present. The value of innerHeight is taken from the height of the window's layout viewport.

What is browser height?

Browser height is the number of pixels within the viewable browser space, while screen height is the height of the entire monitor in pixels.


1 Answers

You'll want something like this, taken from http://www.howtocreate.co.uk/tutorials/javascript/browserwindow

function alertSize() {   var myWidth = 0, myHeight = 0;   if( typeof( window.innerWidth ) == 'number' ) {     //Non-IE     myWidth = window.innerWidth;     myHeight = window.innerHeight;   } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {     //IE 6+ in 'standards compliant mode'     myWidth = document.documentElement.clientWidth;     myHeight = document.documentElement.clientHeight;   } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {     //IE 4 compatible     myWidth = document.body.clientWidth;     myHeight = document.body.clientHeight;   }   window.alert( 'Width = ' + myWidth );   window.alert( 'Height = ' + myHeight ); } 

So that's innerHeight for modern browsers, documentElement.clientHeight for IE, body.clientHeight for deprecated/quirks.

like image 192
meder omuraliev Avatar answered Sep 30 '22 23:09

meder omuraliev