- JavaScript — Get Browser Height
- 10 Answers 10
- Get Chrome Browser Height or NavMenuToolbars Height
- Questions : Get Chrome Browser Height or NavMenuToolbars Height
- Answers 1 : of Get Chrome Browser Height or NavMenuToolbars Height
- How would I go about calculating the window toolbar area height?
- 1 Answer 1
- How do i calculate the height of toolbars, address bars and other navigation tools in pixels?
- 4 Answers 4
- Tested working in the following browsers :
- Using JQuery :
- Without JQuery
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.
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?
10 Answers 10
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.
window_size = $(window).height();
You can use the window.innerHeight
We don’t need to support Internet Explorer 🙂 If you must, try document.documentElement.clientHeight or using jquery instead.
The way that I like to do it is like this with a ternary assignment.
var width = isNaN(window.innerWidth) ? window.clientWidth : window.innerWidth; var height = isNaN(window.innerHeight) ? window.clientHeight : window.innerHeight;
I might point out that, if you run this in the global context that from that point on you could use window.height and window.width.
Works on IE and other browsers as far as I know (I have only tested it on IE11).
Super clean and, if I am not mistaken, efficient.
I’m interested to know if there is a technical reason why the pattern var width = window.innerWidth || window.clientWidth; is not used.
Well the first reason is that only one of the two properties is defined, depending on the browser type used. As for why not just use the coalesce operator, technically that should be used when the items are defined, but one of them might be some false value. Chrome is smart enough to still give a value, but at least historically, Firefox would complain that the first variable doesn’t exist. Not sure how it is now, but that’s the reason why.
Ok, just for reference, I tested this in the latest versions of Firefox, Chrome, Safari, and IE Edge. Using coalesce on undefined variables works fine, so it looks like that issue has been fixed. So with that in mind, the only reason you would prefer using ternary statements over the coalesce form is for personal style reasons, or so you can support older browsers. Keep in mind that when this andwer was written, we had to support IE6 and FF3, and if I had to guess, I would say it was FF3 that had the issue with the coalesce syntax.
There’s a simpler way than a whole bunch of if statements. Use the or (||) operator.
function getBrowserDimensions() < return < width: (window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth), height: (window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight) >; > var browser_dims = getBrowserDimensions(); alert(«Width = » + browser_dims.width + «\nHeight mt24″>