View Full Version : Question about defining and calling functions in a .js file

06-27-2012, 04:40 PM
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;

Old Pedant
06-27-2012, 10:07 PM
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.

06-28-2012, 06:08 AM
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?)

06-28-2012, 11:43 PM
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.

Old Pedant
06-29-2012, 12:26 AM
HTML doesn't have functions. What in the world are you talking about?

I think you are very badly confused.