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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Question about defining and calling functions in a .js file

    Hello everyone,

    I have a .js file where I'm trying to define a function. I want to then call the same function in an HTML page where I will call defined variables within the .js file itself to return a yes/no output. At the bottom of this post is the function itself, taken from another website, and at present when I load the webpage it causes problems.

    If you are curious I am using this code to produce a black or white text based on different colors entered and I'm using it as a 'database' for a bunch of different files. To save a great deal of time, I'd like to include this function which would remove the hassle of putting in a yes / no entry for every single file. Thank you for reading.



    function brighttest(x)
    {

    String fontColor = x;

    // 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) {
    var fcolor = "#FFFFFF";
    return fcolor;
    } else {
    var fcolor = "#000000";
    return fcolor;
    }
    }

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,857
    Thanks
    78
    Thanked 4,417 Times in 4,382 Posts
    Ummmm...what you have there is not JavaScript.

    It looks like it is Java, until you get to the bottom where it uses var, which isn't legal Java. Oh, and the way the function is defined isn't legal Java, either.

    So I have no idea what language it actually is.

    Are you sure you didn't try to take somebody's Java code and make it into JavaScript code???? That will never even come close to working.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh, thank you...


    Well I guess my next question would be, is it possible to define the function at the beginning of the code and call the html function from javascript? (I'm guessing somehow passing the function as a type of variable?)

  • #4
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The simplest solution would probably be to define the function in HTML then return a value from java script to HTML and call the function using that returned value.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,857
    Thanks
    78
    Thanked 4,417 Times in 4,382 Posts
    HTML doesn't have functions. What in the world are you talking about?

    I think you are very badly confused.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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