tags:

views:

159

answers:

4

I need to blink between two colors:red and white.

I've found that after $targetcss('background-color','red'),

value of $targetcss('background-color') is not red,but rgb(255, 255, 255),

pay attention that there is a space between "," and digit in firefox,

but there is no space in IE.

And my code is now :

if(existingBgColor != 'rgb(255, 255, 255)')
    $target.css('background-color', 'rgb(255, 255, 255)');
else 
    $target.css('background-color', 'white');

Which works in firefox,but no for IE.

Of course I can change it to this to make it apply for IE:

if(existingBgColor != 'rgb(255, 255, 255)' && existingBgColor != 'rgb(255,255,255)')
    $target.css('background-color', 'rgb(255, 255, 255)');
else 
    $target.css('background-color', 'white');

But what for a third browser?

Is there a standard solution for this?

A: 

You could simply trim out all the whitespace before testing:

existingColor = existingColor.replace(new RegExp(/[" "]/g), ""); //rgb(255,255,255)

edit: as argued in the comments: Hex values are cross browser, and very easy to use. But if you insist on rgb values, then the above code will help you sort out your issues. However. If you start writing stuff like $myElm.css("background-color", "red");, you're gonna get a whole new set of issues...

peirix
In fact I'm expecting some way to use colors like "red","white",but seems it's the quick fix for now.
Shore
Bad choice. Regular expressions in JavaScript are slow. Use HEX (take a look at my answer).
roosteronacid
Regular expression for a 10-20 character length string is not slow. It can't be. And your answer didn't answer his question, as he was looking for ways to check rgb values. You gave him another option.
peirix
@peirix - the OP didn't ask for a way to check RGB values per se, but a standard color definition across browsers, which hex color values are
Russ Cam
Yeah, but the question was about rgb values. You gave him another option, and that's fine. I don't deserve a downvote for giving him an answer to his question, even though it might not be the most optimal. If he wants to use rgb values, then your answers aren't really helping.
peirix
@peirix- I want to be clear that I didn't downvote you, as your answer provides a solution. But there is a standard solution for this using hex color values which avoids regular expressions and a string replace. The OP didn't say that he wants to use RGB values specifically, but did ask if there was a standard solution across browsers, which there is.
Russ Cam
A: 

can you use hex values? so red = #ff0000 and white = #ffffff

Wayne Austin
A: 

Use HEX.

Take a look at kuler as well.

roosteronacid
Oh,no,it's not working!
Shore
You have to add "#" in front of the numbers--"#000000" for black, "#FFFFFF" for white, "#FF0000" for red, "#0000FF" for blue, etc.
roosteronacid
A: 

You can specify a hex color value, for example #FF0000 for red

$target.css('background-color', '#FF0000'); // red background color

Here's a Working Demo to play with. Check the results in Firebug or Web Developer toolbar

Russ Cam