By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,567 Members | 1,592 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,567 IT Pros & Developers. It's quick & easy.

Hidden elements have zero clientWidth and clientHeight in Firefox! Is there a workaround?

P: n/a
Hello,

I am writing a function to reposition an element based on whether one
of its edges is outside the visible area of the browser window (the
code is below). My client has asked for code that runs in IE6 and
Firefox. However, I am having a problem in Firefox.

I've recently discovered that Firefox considers the clientWidth and
clientHeight of hidden elements to be "0". I am getting this result on
elements with the CSS property value "display: none". As soon as I
give the elements "display: block," then both clientWidth and
clientHeight contain their expected values again. I've tried hiding
elements on several different pages, and each time I have experienced
the same problem with getting their dimensions.

Unfortunately, I can't just make the elements visible _before_ I get
their dimensions. If I did that, then the elements would be visible
before they have been positioned, which creates an ugly "flicker"
effect.

I have not had any trouble getting the dimensions of hidden elements in
IE. Assuming this is a known issue in Firefox, does anyone know of a
workaround?

Thanks for reading my post,
Noah

===CODE===
This function does not position elements in Firefox that have CSS
"display: none". I've cleaned up my actual production code a bit for
readability's sake; but I would be happy to post the ugly original too
if that would help :)

function aboveTheFold (thingToPosition, leftPosition, topPosition) {
//get element clientWidth and clientHeight
var elWidth = document.getElementById(thingToPosition).clientWid th;
var elHeight =
document.getElementById(thingToPosition).clientHei ght;
//get window width and height, from an external function:
var windowWidth = getWindowHeight();
var windowHeight = getWindowWidth();
//keep popups on screen and above the fold
if ((leftPosition + elWidth) > (windowWidth)) {
leftPosition = windowWidth - elWidth;
}
if ((topPosition + elHeight) > (windowHeight)) {
topPosition -= (topPosition + elHeight) - windowHeight;
}
document.getElementById(thingToPosition).style.lef t=(leftPosition);
document.getElementById(thingToPosition).style.top =(topPosition);
}

Jun 23 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Noah Sussman wrote:
I've recently discovered that Firefox considers the clientWidth and
clientHeight of hidden elements to be "0". I am getting this result
on elements with the CSS property value "display: none".


I'm surprised you don't see the same problem in IE, as it also exists, IIRC.

The solution is to set these properties via script in this order:
obj.style.position='absolute';
obj.style.visibility='hidden';
obj.style.display='block';
<retrieve dimenions here>
obj.style.visibility='';

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Jun 23 '06 #2

P: n/a
Matt Kruse wrote:
I'm surprised you don't see the same problem in IE, as it also exists, IIRC.


Upon closer examination, the code I posted did cause a "flicker" when
repositioning windows in IE. It was just less broken than in FF :)

Thanks for the solution! I just tested my updated code in current
versions of IE, FF and Safari; it works smoothly in all three.

Cheers,
Noah

Jun 23 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.