backgroundColor and Firefox

R

RobG

When I set the background colour of an element using
tdRef.style.backgroundColor and then read it back, Firefox always
gives rgb(r, g, b) regardless of whether I've used rgb(...) or #rrggbb
to set it.

If I use tdRef.bgColor to set/read the value, I always get #rrggbb
regardless of whether I've used rgb(...) or #rrggbb.

IE, on the other hand, when using style.backgroundColor reports back in
whatever format was used (either rgb(...) or #rrggbb), but, like
Firefox, always gives #rrggbb for the bgColor method.

My question is which method is most consistent across various browsers?
I want to use style.backgroundColor (since some browsers don't support
bgColor, I guess it's a legacy from the ver 4 browser days). If I
decide to use rgb(...), is it consistently supported by other browsers
or do some report in #rrggbb regardless?

A bit of play code follows...

Cheers, Rob.

<script type="text/javascript">
function setColor(x) {
x.style.backgroundColor='#003366';
//x.style.backgroundColor='rgb(0, 51, 102)';
alert(x.style.backgroundColor);
}
function setColor2(x) {
x.bgColor='rgb(0, 51, 102)';
alert(x.bgColor);
}
</script>
<table border="1"><tr>
<td width="20px" height="20px"
onclick="setColor(this);"></td>
</tr><tr>
<td width="20px" height="20px"
onclick="setColor2(this);"></td>
</tr></table>
 
T

Thomas 'PointedEars' Lahn

RobG said:
When I set the background colour of an element using
tdRef.style.backgroundColor and then read it back, Firefox always
gives rgb(r, g, b) regardless of whether I've used rgb(...) or #rrggbb
to set it.
Confirmed.

If I use tdRef.bgColor to set/read the value, I always get #rrggbb
regardless of whether I've used rgb(...) or #rrggbb.

You must not use "rgb(...)" on that property since it reflects an HTML
attribute and not a CSS property. "rgb(...)" is allowed in CSS only.
IE, on the other hand, when using style.backgroundColor reports back in
whatever format was used (either rgb(...) or #rrggbb), but, like
Firefox, always gives #rrggbb for the bgColor method.

My question is which method is most consistent across various browsers?

<http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-88135431>
I want to use style.backgroundColor (since some browsers don't support
bgColor, I guess it's a legacy from the ver 4 browser days).

You are confusing browser version and DOM. The `bgColor' property is part
of the W3C HTML DOM, *originating* from implementations in most-used
browsers at the time, particularly, but not limited to, so-called "ver 4
browsers" (I suppose you mean IE 4 and NS 4; note that e.g. IE 5+ is still
only Mozilla/4.0 compatible). The `backgroundColor' property is part of
the W3C Style/CSS DOM.
If I decide to use rgb(...), is it consistently supported by other
browsers or do some report in #rrggbb regardless?

Writing: The HTML attribute property should support the "#rrggbb" syntax
(and reject other values except color names), while the CSS property should
support "#rrggbb", "#rgb", "rgb(...)" values and color names.

Reading: The HTML attribute property should return only the "#rrggbb" value
or a color name, while you have to check (e.g. using a regular expression)
of what format the read value of the CSS property is. By chance, I have
recently hacked on a method to convert "rgb(...)" to "#rrggbb" named
rgb2HexCode() and contained in <http://pointedears.de/scripts/string.js>
(use <http://pointedears.ml-webhosting.de/scripts/string.js> in case of a
timeout of the former).

Unfortunately, it does not work yet with Opera 7. And some optimizations
and enhancements are yet to be done.


HTH

PointedEars

P.S.: I could not reply via e-mail, please verify your sender address.
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top