PDA

View Full Version : JavaScript to display window size



PapaGeek
28 Apr 2010, 02:48 PM
I wrote the following JavaScript to display the screen resolution and the size of the browser window:

height = screen.height;
width = screen.width;
document.write( width + " x " + height + "<br>");
if (document.layers) {
width = window.innerWidth;
depth = window.innerDepth;
}
// Explorer 6 Strict Mode
else if (document.documentElement && document.documentElement.clientHeight) {
width = document.documentElement.clientWidth;
depth = document.documentElement.clientHeight;
}
else if (document.all) {
width = document.body.clientWidth;
depth = document.body.clientHeight;
}
document.write( width + " x " + depth);

The size of the window always displays as 0 x 0 when the page is displayed, then displays the correct values when the page is refreshed.

What is the proper way to write this script?