RobG wrote:
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>
<http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSS2Properties-backgroundColor>
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.