1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Function in .js file prevents rest of table images and words from functioning

Discussion in 'JavaScript' started by victorsaur, Jul 10, 2012.

  1. #1
    I've defined this function in a .js file. It's supposed to take a color, make it the background color in a table cell, and adjust the color of the text depending on whether the color is dark or light. What is wrong with the code? [​IMG]

    function brighttest(season)
    {

    String fontColor = season;

    // remove hash character from string
    String rawFontColor = fontColor.substring(1,fontColor.length());

    // convert hex string to int
    int rgb = Integer.parseInt(rawFontColor, 16);

    Color c = new Color(rgb);

    float[] hsb = Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), null);

    float brightness = hsb[2];

    if (brightness < 0.5) {
    document.write('<td width="25%" bgcolor="'+season+'">');
    document.write ('<font color="#FFF8C6">'+season+'</font>'); //prints the value
    } else {
    document.write('<td width="25%" bgcolor="'+season+'">');
    document.write ('<font color="#000000">'+season+'</font>'); //prints the value
    }
    }
     
    victorsaur, Jul 10, 2012 IP
  2. jhine

    jhine Active Member

    Messages:
    25
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    53
    #2
    You seem to be confusing JavaScript with Java.
     
    jhine, Jul 10, 2012 IP
  3. Unni krishnan

    Unni krishnan Peon

    Messages:
    237
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    0
    #3
    Are you getting any errors in the Console ?
     
    Unni krishnan, Jul 10, 2012 IP
  4. victorsaur

    victorsaur Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It causes all other links to images and text to not show up... and I'm sure that it is not the calling of the function itself, rather the code of the function that has some error.


    EDIT:
    I'm not 100% sure whether the copied part (everything before the if command) is in fact javascript or java. I copied most of this code online, which may be causing me to confuse javascrpit with Java (after considering jhine's response)... It is stated on the original site that this code "
    uses the
    java.awt.Color
    class to derive the brightness value of the colour".
    If this is in fact a java question, then I'd request the admin to move it to the appropriate forum.
     
    Last edited: Jul 11, 2012
    victorsaur, Jul 11, 2012 IP
  5. Unni krishnan

    Unni krishnan Peon

    Messages:
    237
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    0
    #5
    Thats strange. You are using the java 'awt' libraries. Along with Javscript's 'document.write' function.

    If u want to do this in JS, here are a number of JS libraries which you can use to implement what u want. No need of coding it.
    eg: Colors.js, JSColor etc..

    You can find a list from here http://www.queness.com/post/10081/11-javascript-color-pickers-plugins
     
    Unni krishnan, Jul 11, 2012 IP
  6. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #6
    The code is blowing up at the 3rd line:

    String fontColor = season;

    since that's not valid Javascript. Nothing after that will even be seen. If you're looking at this in IE you should see a notice at the bottom left corner of the browser that there's a problem. In Firefox with Firebug, you'll get a script error pointing at that line of code.

    You have to rewrite your code in Javascript. What you wrote, as jhine said, is Java, a totally different programming language (which won't run in web browsers).
     
    Rukbat, Jul 13, 2012 IP