Determining the Window Size


with (document.body) {   window.alert(clientWidth + " x " +                clientHeight + " pixels"); } 

Accessing the properties of the current window largely depends on the browser used. As is often the case with JavaScript, the browser world is divided into two groups: Internet Explorer and the rest. With Internet Explorer, the width of a window can be determined via document.body.clientWidth; other browsers use window.innerWidth for the "inner" width (excluding window decoration) and window.outerWidth for the "outer" width (including window decoration). For determining the height, just replace width and Width with height and Height, respectively.

The following code outputs the current window's dimensions:

Determining the Current Window's Dimensions (dimensions.html)

<script language="JavaScript"   type="text/javascript"> window.onload = function() {   if (window.innerWidth) {     window.alert("inner: " + window.innerWidth +       " x " + window.innerHeight +       " pixels\nouter: " + window.outerWidth +       " x " + window.outerHeight + " pixels");   } else {     with (document.body) {       window.alert(clientWidth + " x " +         clientHeight + " pixels");     }   } } </script> <body></body> 

Tip

Note that the page needs a <body> area and the code must not be executed before the page has been fully loaded for the code to work in Internet Explorer.





JavaScript Phrasebook(c) Essential Code and Commands
JavaScript Phrasebook
ISBN: 0672328801
EAN: 2147483647
Year: 2006
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net