Posted on Tuesday, 18th August 2009 by chris

<script type="text/javascript">
<!--
 
 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
 }
document.write('<p>Your viewport width is '+viewportwidth+'x'+viewportheight+'</p>');
//-->
</script>

Posted in JavaScript | Comments (3)

3 Responses to “Get viewport size (width and height) with javascript”

  1. Kouba Says:

    I liked it. So much useful material. I read with great interest.

  2. Clemento Says:

    Interesting and informative. But will you write about this one more?

  3. Cornelius Says:

    Thank you! You often write very interesting articles. You improved my mood.



Leave a Reply

You must be logged in to post a comment.