Determining Screen Size


window.alert(screen.availWidth + " x " +              screen.availHeight + " pixels"); 

In an ideal world, all web layouts would be flexible and would adapt to the currently available solution. Unfortunately, this is not the case, but you may be able to adapt with JavaScript.

The screen object (window.screen in recent browsers; just screen in older ones) provides information about the current screen resolution. The properties you will be interested in are availableHeight and availableWidth, providing the height and the width available. This can, for instance, be used for determining the current resolution and redirecting to a page suitable for those screen dimensions.

The preceding code (file screen.html) determines the current available screen space. Figure 9.3 shows the result using a 1024x768-pixel solution.

Figure 9.3. The screen dimensions may not be what you expected.


Note

Note that in the specific example 30 pixels are missingthey "belong" to the Microsoft Windows bar (similar effects are achieved when this code sample is run on other systems with system bars). If you are interested in the actual width and height, use the width and height properties of the screen object.





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