Connecting Tech Pros Worldwide Help | Site Map

backgroundColor and Firefox

RobG
Guest
 
Posts: n/a
#1: Jul 23 '05

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>

Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#2: Jul 23 '05

re: backgroundColor and Firefox


RobG wrote:
[color=blue]
> 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.[/color]

Confirmed.
[color=blue]
> If I use tdRef.bgColor to set/read the value, I always get #rrggbb
> regardless of whether I've used rgb(...) or #rrggbb.[/color]

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.
[color=blue]
> 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?[/color]

<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>
[color=blue]
> 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).[/color]

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.
[color=blue]
> If I decide to use rgb(...), is it consistently supported by other
> browsers or do some report in #rrggbb regardless?[/color]

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.
Closed Thread