473,513 Members | 2,684 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

backgroundColor and Firefox


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>

Jul 23 '05 #1
1 14142
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.
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
6598
by: Philip | last post by:
Hey, Is it possible to get the default value of an element's style.backgroundColor property that's set in a css class? Example, since I'm very tired and can't word that any better ;) ...
13
6052
by: cosmic foo | last post by:
if i change the backgroundColor of a tag to 'red', it works in both ie and mozilla. if i change the backgroundColor of a tag to 'redd', i get 'invalid property value' in ie, and no error in...
87
9495
by: expertware | last post by:
Dear friends, My name is Pamela, I know little about CSS, but I would like to ask a question I have an image on a web page within a css layer: <DIV ID=MyLayer STYLE = "position:...
14
2298
by: expertware | last post by:
Ok! to avoid confusion I will start a new argument. Thanks!! FIREFOX 1.0.7 AND IE6 viewed through DATATIME: a summary REPORT ===============================================================...
2
2798
by: LS | last post by:
Does anybody know if (and then HOW!) can I change the background color of a WindowsForms.TabControl control? It doesn't have any "BackgroundColor" property, how could then I change it? Thanx!!
5
3106
by: SPE - Stani's Python Editor | last post by:
Hi, During optimizing SPE for Ubuntu, I found something strange. I have Ubuntu 5.10 "The Breezy Badger" and unfortunately this code is not working: >>> import webbrowser >>>...
6
10691
by: fallen angel | last post by:
halu there!! i'm a 1month experienced java scripter but got a good hang of it already. hehehe.. i developed a "color selection" tool, similar to the one in flash when you choose a color for a...
10
2227
by: Cliff Cotterill | last post by:
I've tried multiple ways of setting the background-color of a DropDownList, but none seem to work. How can the color be set? I've tried setting the BackGroundColor of the DrowDownList component,...
4
1648
by: André | last post by:
Hi, i created a table with cells. One cell has background-color (LightSlateGray) and contains a label without text but also with a backgroundcolor (red). The width of the label is set by a value...
0
7379
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,...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7521
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
5682
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
5084
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
4745
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
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1591
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
455
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.