View Full Version : cannot find Font Color value using document.all

08-09-2004, 11:03 PM
:rolleyes: Before I begin, I'm trying to take an existing font color value and change it so it displays as another color. I'm using Domino's Search function, so I can't just change the source, since I have no control over it. However, I want to change the color of the highlighted text from "green" to something else. My problem is I can't see the "green" font color items, only everything else. Now the for the code.

I have some HTML where I cannot find certain FONT Colors using the document.all.tags... The code snippit is as follows:

<tr valign="top"><td width="120">
<font size="2" color="#000080"><font color="green"><b>Policy</b></font> Name:</font>
</td><td width="168" bgcolor="#FFFFFF"><font size="2">Emergency Codes</font></td></tr>

When I use this javascript code (minus the periods for formatting):

var test1
var allColor = document.all.tags( "FONT" )
for (var i = 0; i < allColor.length; i++ ) {
.. test2 = allColor[i].attributes
.. for (var j = 0; j < test2.length; j++ ) {
..... if (test2.item(j).nodeName == "color") {
........ test1 = test1 + ";" + test2.item(j).nodeValue
..... }
.. }
alert( test1 )

I end up getting a long list of the Font colors in the document I'm searching. With the HTML code segment above I get the font color value "000080" in the prompt but I don't get any of the "green" values in this list. Is there something I'm missing here? I thought 'all' searches through every item, regardless if its embedded in another or not. Help, help, help. I feel I'm so close and yet I've racked my brain over this one.

Any fresh and more brilliant brain out there willing to help? Thanks every so much. :thumbsup:


08-10-2004, 06:33 AM
Let's do this nicely:

var fontTags = document.getElementsByTagName("font");
for (var i = 0; i < fontTags.length; i++) {
if (fontTags[i].getAttribute("color") == "green") {
fontTags[i].setAttribute("color", "red');

That will change all green's to red. Your use of <font> tags is dubious at best too. They are deprecated, and CSS serves as a much better replacement, but that is another topic for another thread.

08-10-2004, 06:19 PM
Thanks JKD for your reply and helping simplify my delirious code. :) I tried the code you included and I still get the same problem. This is what I get returned in an alert when I add up every "fontTags[i].getAttribute("color")" into a variable:

;#000080;#000080;#000080;#000080;;#000080;#0000ff;#000080;#000080;;;#000080;;;;;#000080;#000080;#ff0 000;

Even though the HTML code says "green" as the code example I gave earlier, it does not see it. I'm guessing the extra ";" are where green might be, but it's not there. #ff0000 is red, #000080 is dark blue, and #0000ff is lighter blue. It seems to be grabbing every other font but the named font color. Any ideas? :confused:

Thanks again to everyone who reads this and especially for anyone who can help me on this one. :thumbsup:

08-11-2004, 05:41 AM
I knew that Gecko normalized color's to RGB, but I didn't think IE did. At least, CSS-wise, but <font> is wholly another beast. So here's what is going on:

It appears that within a <font color> declaration, the color is normalized to a 6 hexadecimal digit string. I'm assuming that "#f00" would show up as "#ff0000" too. There is no real way around this normalization, unless you try looking at the source itself and parsing it via Javascript.

A superior workaround would simply be to simply use CSS and a <span> tag with a class, then dynamically change style rules within the class via DOM2 CSS and IE's proprietary (and practically equivalent) extensions.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum