View Full Version : document.body.clientWidth

07-11-2002, 08:53 PM
i need to find out the inner width of a browser window. i'm using window.innerWidth for ns4.x and it works fine.
for ie i am trying to use document.body.clientWidth, but i get an error saying "document.body.clientWidth is not an object" (on a mac) or "objet expected" (on a pc). I have also tried using document.getElementById('body').clientWidth, but it would generate the same error message.
i finally went to javascriptkit.com wherethere is a tutorial on "Using JavaScript to statically display elements" (http://javascriptkit.com/javatutors/static.shtml) and it says there to use the same expressions I am using. On page 3 of teh tutorial (http://javascriptkit.com/javatutors/static3.shtml) there is an example that actually utilizes document.body.clientWidth and it works in my browser with no error messages whatsoever. so why would teh same expression give me an error on another page? the page was working fine before document.body.clientWidth was added to it, so I know it is not an error from something above that piece of code.
here's the script:

var wdth
if (document.layers) {
wdth = window.innerWidth
else {
wdth = document.body.clientWidth

am i just not seeing something obvious???

07-11-2002, 09:06 PM
if i remember correctly, it's window.body.clientWidth. the link to Microsoft in my sig will tell you for sure.

07-11-2002, 09:58 PM
here's a piece of script from msdn:
Center the object horizontally:
object.style.left=(document.body.clientWidth/2) - (object.offsetWidth/2);
doh, i really cannot think of a reson for the thing not to work!

07-11-2002, 10:05 PM
are you trying to use this information before the onload event has fired?

07-11-2002, 10:14 PM
i have no onload events on my page.
i am trying to use it inside a .js page wichi is linked to from the <body> of the html document.

07-11-2002, 10:21 PM
i think that's your problem, then. i could be misremembering, but i think that IE throws a fit if you try access those properties, before the page has finished loading. trying moving that code into an onload event handler, if possible.

if that doesn't solve it, then i have no idea.

07-12-2002, 03:51 PM
You were right - once moved into the onLoad - it worked!
now i need to think how to accomodate my script with that, but i have an idea already.