IE 6 - clientWidth sometimes 0!?

T

Tom

Hi,

I have an iframe that gets its own width from document.body.clientWidth
(on onLoad).

In Firefox, clientWidth always has an expected value, but in IE 6 it's
sometimes 0! It seems to depend on the loading of the containing page,
because in local tests IE 6 works fine. Also works when refreshing the
iframe after the containing page has loaded.

Is there a better way to get the width of an iframe from _within_ the
iframe? I've tried scrollWidth, offsetWidth and
parent.framename.frameElement.width -- latter works but only if
containing page is in same domain as iframe, which isn't always the
case.

window.setTimeout to delay the script would be a last resort.

Thanks for any ideas.

Thomas
 
D

Dietmar Meier

Tom said:
window.setTimeout to delay the script would be a last resort.

You should use the document's readyState property instead:

window.onload = function() {
if (typeof document.readyState == "undefined") {
myFunction();
}
else {
if (document.readyState == "complete") {
myFunction();
}
else {
document.onreadystatechange = function() {
if (document.readyState == "complete") {
document.onreadystatechange = null;
myFunction();
}
}
}
}
}
....
function myFunction() {
// ... do something with document.body.clientWidth ...
}

ciao, dhgm
 
T

Tom

Oops... no, it doesn't!

I checked the readyState within the myFunction function and even when
it is "complete" it often doesn't get a correct value for clientWidth.

If I delay the function by 50ms or so it seems to work reliably. I
think it may have something to do with the local caching of the page,
because I have specifically enabled that (and it's a necessity).

Really, really weird.
 
T

Tom

Ok might as well just follow up with my final solution (sorry :))

I'm calling a function that loops every 10ms until a non-zero value is
found for clientWidth, because the time before the value becomes
available seems to depend on the containing page's load time. Evidently
IE is loading and parsing the iframed page from cache before it even
knows how big it's going to be. Arg.
 
D

Dietmar Meier

Tom said:
I checked the readyState within the myFunction function and even when
it is "complete" it often doesn't get a correct value for clientWidth.

Then IMHO that's an IE bug. This kind of properties may change while
readyState is "interactive", but should not be changed any more when
it's "complete".

ciao, dhgm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top