PDA

View Full Version : Finding out active window dimensions: Javacript



lyew
July 1st, 2001, 20:23
Hi,

I've got a Javascript problem. I'm trying to find out the active browser window size that a client is using to retrieve my web page. I tried using

window.screen.width (and height) but that returns the maximum resolution of the client's monitor.

Does anyone know how that can be done?

The second question is how do I use Javascript to find out where certain elements are (coordinate-wise) on my client's browser window. If I have a table that is set to "x%" I won't know what the coordinates are for each of its <td> elements. Is there anyway of finding that out?

Thanks.

ashben
July 1st, 2001, 23:19
if (document.layers) {
// IE
width = window.innerWidth;
height = window.innerHeight;
}
else if (document.all) {
// Netscape
width = document.body.clientWidth;
height = document.body.clientHeight;
}

lyew
July 2nd, 2001, 02:00
Thanks, Ashben.

I tried width = window.innerWidth and height = window.innerHeight through IE 5.5

Both returned the value, "undefined."

:(

ashben
July 2nd, 2001, 05:39
I messed it up. The following should work fine:

if (document.layers) {
//Netscape 4+
width = window.innerWidth;
height = window.innerHeight;
}
else {
// IE 4+
width = document.body.clientWidth;
height = document.body.clientHeight;
}

lyew
July 2nd, 2001, 07:14
Ashben,

It didn't work. This time IE 5.5 returned an "object required error" with the code.

This is what I did:

...
<title>Untitled</title>
<script language="javascript">
max_X = document.body.clientWidth;
max_Y = document.body.clientHeight;
</script>

</head>

<body>

The maximum x value of this window is <script>document.write(max_X);</script><br>
The max y value of this window is <script>document.write(max_Y);</script><br>


</body>

ashben
July 2nd, 2001, 09:53
Aparently, this technique only works when executed once the document body is parsed. The following works for me:

<script language="javascript">
function init() {
x = document.body.clientWidth;
y = document.body.clientHeight;
alert(x);
alert(y);
}
</script>
</head>

<body onload="javascript:init();">


Hope it help!

lyew
July 3rd, 2001, 07:24
Thanks again Ashben. It did work this time. I realized my problem was that I placed the <script></script> before the <body> tag. That's why IE 5.5 kept returning "document.body.ClientWidth" is null or not an object.

When I placed the script below the <body> tag, it worked fine.