Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the exact height and width of the viewport in a cross-browser way (no Prototype/jQuery)

I'm trying to find the exact height and width of a browser's viewport, but I suspect that either Mozilla or IE is giving me the wrong number. Here's my method for height:

var viewportHeight = window.innerHeight ||                       document.documentElement.clientHeight ||                       document.body.clientHeight; 

I haven't started on width yet but I'm guessing it's going to be something similar.

Is there a more correct way of getting this information? Ideally, I'd like the solution to work with Safari/Chrome/other browsers as well.

like image 307
Alex Grin Avatar asked Nov 19 '09 21:11

Alex Grin


People also ask

How do you find the width and height of a viewport?

You can use the window. innerHeight property to get the viewport height, and the window. innerWidth to get its width. let viewportHeight = window.

How do I find my browser width and height?

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

How do I find my current viewport height?

Getting viewport height To detect interior height of the window in pixels we can use innerHeight property. window. innerHeight returns height of window including the scrollbar. To exclude scrollbar we need to use the root <html> element's clientHeight property instead.

How do you adjust the height of a viewport?

A simple way to solve this is to use the viewport percentage unit vh instead of %. One vh is defined as being equal to 1% of a viewport's height. As such, if you want to specify that something is 100% of the latter, use " height: 100vh ". Or if you want to make it half the height of the viewport, say " height: 50vh ".


2 Answers

You might try this:

function getViewport() {   var viewPortWidth;  var viewPortHeight;   // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight  if (typeof window.innerWidth != 'undefined') {    viewPortWidth = window.innerWidth,    viewPortHeight = window.innerHeight  }  // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)  else if (typeof document.documentElement != 'undefined'  && typeof document.documentElement.clientWidth !=  'undefined' && document.documentElement.clientWidth != 0) {     viewPortWidth = document.documentElement.clientWidth,     viewPortHeight = document.documentElement.clientHeight  }   // older versions of IE  else {    viewPortWidth = document.getElementsByTagName('body')[0].clientWidth,    viewPortHeight = document.getElementsByTagName('body')[0].clientHeight  }  return [viewPortWidth, viewPortHeight]; } 

( http://andylangton.co.uk/articles/javascript/get-viewport-size-javascript/ )

However, it is not even possible to get the viewport information in all browsers (e.g. IE6 in quirks mode). But the above script should do a good job :-)

like image 170
Leo Avatar answered Oct 16 '22 19:10

Leo


You may use shorter version:

<script type="text/javascript"> <!-- function getViewportSize(){     var e = window;     var a = 'inner';     if (!('innerWidth' in window)){         a = 'client';         e = document.documentElement || document.body;     }     return { width : e[ a+'Width' ] , height : e[ a+'Height' ] } } //--> </script> 
like image 32
dzona Avatar answered Oct 16 '22 19:10

dzona