MattsBits
MattsBits

MattsBits

Get The Width Of The Browser Window  

by Matt Hawkins, 26/04/2009
Categories : JavaScript

Sometimes it is useful to obtain the dimensions of the users browser window. The following JavaScript code can be used to obtain the height and width :

<script type="text/javascript">
<!--

  var v_width;
  var v_height;
  
  // Standards compliant browsers (mozilla/netscape/opera/IE7)
  // use window.innerWidth and window.innerHeight
  
  if (typeof window.innerWidth != 'undefined')
  {
    v_width = window.innerWidth,
    v_height = window.innerHeight
  } else if (typeof document.documentElement != 'undefined'
          && typeof document.documentElement.clientWidth != 'undefined'
          && document.documentElement.clientWidth != 0) {
    // IE6 in standards compliant mode
    // (i.e. with a valid doctype)
    v_width = document.documentElement.clientWidth,
    v_height = document.documentElement.clientHeight
  } else {
    // older versions of IE
    v_width = document.getElementsByTagName('body')[0].clientWidth,
    v_height = document.getElementsByTagName('body')[0].clientHeight
  }
  document.write('<p>Viewport size : ' + 
                  v_width + 'x' + 
                  v_height + '</p>');

//-->
</script>

Author : Matt Hawkins  Last Edit By : Matt Hawkins
PHP Powered  MySQL Powered  Valid XHTML 1.0  Valid CSS  Firefox - Take Back The Web  EUKHost - Recommended Webhosting Solutions

MattHawkins CMS v3.0 - Copyright 2009-2017