473,498 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
2 10956
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
7224
by: BT | last post by:
Hi. I like to know where and how do you set the ClientHeight and ClientWidth properties? I can see Height,Width,Top,Left in the Properties window. Any suggestion would be very helpful. ...
2
5277
by: Jon | last post by:
Hi all, I am trying to create a page that contains a number of div elements, with links on the left side of the page allowing the user to select which div to display. Some of the pages contain...
2
1779
by: X t l a n | last post by:
Hello. I've noticed that Mozilla browsers older than 1.1 release don't support document.body.clientWidth but I would like to know dimensions of client workspace area. How to get them?...
7
1557
by: Luke Matuszewski | last post by:
Hi. I know that it is hard problem (as hard as CSS and HTML layouting rules apply), but maybe someone has some code or idea how to start it. For now i know i will cssQuery: ...
5
3084
by: Ian Davies | last post by:
Hello I wish to use J.S. to obtain the screen ht and put this value into a hidden field for posting. so that I can then use it in a php file. A simplified script of what I am trying to do is...
10
3772
by: jon | last post by:
I'm trying to use a hidden iframe to print the contents of one div seamlessly. Currently I can create the hidden iframe, copy the contents of the div to the iframe, and print it. I even have a...
2
3342
by: wolfing1 | last post by:
I have the following page that is supposed to self-resize to the image's dimensions when opened, but for some reason it works fine with Firefox, but in IE6 it doesn't. I added the alert and it...
2
10711
by: arman_legend | last post by:
Does anyone know why this returns 0 in Firefox (2) and correct value in IE (7)? alert(document.body.clientHeight); Thank you.
5
2726
by: Doug Gunnoe | last post by:
I'm considering resizing a div onload to better match the screen width of the user. Easy enough, however it seems that I have read in this group that there are potential problems with this,...
5
21890
by: liketofindoutwhy | last post by:
for the code var node = document.getElementById("something") alert(node.offsetWidth) is very similar to node.clientWidth and node.scrollWidth I just wonder why offsetWidth is well...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4908
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4588
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1417
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
290
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.