D
David Birnbaum
Dear comp.lang.javascript,
I need to allow users to enter a color as free text (not by choosing
from a list) and I'd like to allow any color that will be recognized
by the user's browser. This means that users may (inadvertently or
perversely) enter invalid colors (e.g., "green" is okay but
"greenpeace" isn't). Javascript engines seem to know whether a color
is invalid; they can generate some sort of error, instead of just
applying the color and having it fail silently. Firefox (at least)
knows when it's been fed a non-color because it generates an error in
the error console:
Warning: Expected color but found 'koala'. Error in parsing value
for 'color'. Declaration dropped.).
Can I get access to information about whether a color is legit in my
javascript program? I tried:
if (element.style.color = 'koala') { alert('Nope');}
but running the test generates the error, instead of trapping it.
Is there a standard way to verify that the user has entered a color
name (or other notation) that will be recognized as a color by the
javascript engine in the browser? Something like an isColor() test
that would let me check whether the user input will be recognized as a
color before otherwise using it? I can write a function that will do
this, but I can't think of how to do that without hard-wiring a list
of recognized color names, and the recognized names might 1) vary
across browsers and 2) change over time, which means that I might have
to be more pessimistic than an individual user's individual browser
might require. Advice welcome!
Thanks,
David
(e-mail address removed)
I need to allow users to enter a color as free text (not by choosing
from a list) and I'd like to allow any color that will be recognized
by the user's browser. This means that users may (inadvertently or
perversely) enter invalid colors (e.g., "green" is okay but
"greenpeace" isn't). Javascript engines seem to know whether a color
is invalid; they can generate some sort of error, instead of just
applying the color and having it fail silently. Firefox (at least)
knows when it's been fed a non-color because it generates an error in
the error console:
Warning: Expected color but found 'koala'. Error in parsing value
for 'color'. Declaration dropped.).
Can I get access to information about whether a color is legit in my
javascript program? I tried:
if (element.style.color = 'koala') { alert('Nope');}
but running the test generates the error, instead of trapping it.
Is there a standard way to verify that the user has entered a color
name (or other notation) that will be recognized as a color by the
javascript engine in the browser? Something like an isColor() test
that would let me check whether the user input will be recognized as a
color before otherwise using it? I can write a function that will do
this, but I can't think of how to do that without hard-wiring a list
of recognized color names, and the recognized names might 1) vary
across browsers and 2) change over time, which means that I might have
to be more pessimistic than an individual user's individual browser
might require. Advice welcome!
Thanks,
David
(e-mail address removed)