How to read CSS styles - problem

T

Tony

I seem to remember being able to do this, but I can't seem to find out
how I did it before...

Basically, I have styles defined by class in a stylesheet, and I need
to read the "width" style. obj.currentStyle.width works just fine in
IE, but Firefox doesn't recognize it.
document.defaultView.getComputedStyle(obj,null).getPropertyValue('width')
gives me the actual width (just as obj.offsetWidth does), not the width
style assigned by the stylesheet.

Because the elements are cells in a table row, the rendering of the
table is causing the actual width to vary from the assigned width
style. So, how can I get the assigned width style in Firefox (and other
browsers)?

Or am I out of luck on this?
 
T

Thomas 'PointedEars' Lahn

Tony said:
Basically, I have styles defined by class in a stylesheet, and I need
to read the "width" style. obj.currentStyle.width works just fine in
IE, but Firefox doesn't recognize it.

Because it is IE-proprietary.
document.defaultView.getComputedStyle(obj,null).getPropertyValue('width')

AFAIS document.defaultView refers to the same object as `window' and is not
more or less standards compliant (than the latter proprietary property of
the Global Object.)
gives me the actual width (just as obj.offsetWidth does), not the width
style assigned by the stylesheet.

Because the elements are cells in a table row, the rendering of the
table is causing the actual width to vary from the assigned width
style.

Why is that a problem?

ISTM that you have a design problem. Why are you forcing the cells to
a specific width in the first place? Let the UA do its job to compute
what is the appropriate width according to their content and the actual
viewport (or layout box) size.
So, how can I get the assigned width style in Firefox (and other
browsers)?

Retrieve the value of the `className' property of the element and
search through the `cssRules' collection of each element of the
document.styleSheets collection. (You do not want to do that.)
Or am I out of luck on this?

No, but support is still limited, and the approach is rather inefficient.
It is far better to resolve your design problem by reconsidering your
approach.


PointedEars
 
T

Tony

Thomas said:
Because it is IE-proprietary.

I figured as much
AFAIS document.defaultView refers to the same object as `window' and is not
more or less standards compliant (than the latter proprietary property of
the Global Object.)

OK - I got that from Quirksmode, I usually find PPK to be reasonably
reliable on the compliance matters, but he admitted that script left
something to be desired.
Why is that a problem?

ISTM that you have a design problem. Why are you forcing the cells to
a specific width in the first place? Let the UA do its job to compute
what is the appropriate width according to their content and the actual
viewport (or layout box) size.

Easy enough to say, but when the boss says "I want things to happen
this way", you do it.

Believe it or not, we do have reasons for what we're doing - even if it
may not seem that way by some of the questions I ask :)
Retrieve the value of the `className' property of the element and
search through the `cssRules' collection of each element of the
document.styleSheets collection. (You do not want to do that.)

I wound up doing something similar, but not exactly like that. It does
what I need, though. I was hoping there might be a somewhat more
elegant solution.
No, but support is still limited, and the approach is rather inefficient.
It is far better to resolve your design problem by reconsidering your
approach.

Given the nature of the task, that really can't be done.

Thanx for the input.
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top