Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Aug 2004
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy cannot find Font Color value using document.all

    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.

    Tim

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Let's do this nicely:

    Code:
    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.

  • #3
    New Coder
    Join Date
    Aug 2004
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    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?

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

  • #4
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •