By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,570 Members | 1,608 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,570 IT Pros & Developers. It's quick & easy.

is "" a legal color string?

P: n/a
I need to remove colors put on links with Javascript so that CSS hover
works on uncolored links. Both background color and transparent turn
off the hover effect, and null doesn't remove the color. I discovered
that the empty string worked in IE 6, Mozilla 1.1 and Opera 7. But is
that legal? If not, what is the right way? Here's a toy demo of the
working page:

<html><head><title>Example</title>

<style type="text/css">
a:hover { background-color: #FFFF66;}
body { background-color: #FFFFFF; font-size: 24pt;}
</style>

<script type="text/javascript">
var oldLink;

function select(link) {
if (oldLink) oldLink.style.backgroundColor = ""; // OK???
oldLink = link;
link.style.backgroundColor = "#CCCC33";
}
</script>
</head>
<body>
<p>
<a href="#" onclick="select(this)">Line 1</a><br>
<a href="#" onclick="select(this)">Line 2</a><br>
<a href="#" onclick="select(this)">Line 3</a><br>
<a href="#" onclick="select(this)">Line 4</a><br>
<a href="#" onclick="select(this)">Line 5</a><br>
</p>
</body>
</html>
Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
ri******@cs.northwestern.edu (Chris Riesbeck) writes:
I need to remove colors put on links with Javascript so that CSS hover
works on uncolored links. Both background color and transparent turn
off the hover effect, and null doesn't remove the color. I discovered
that the empty string worked in IE 6, Mozilla 1.1 and Opera 7. But is
that legal? If not, what is the right way? Here's a toy demo of the
working page: if (oldLink) oldLink.style.backgroundColor = ""; // OK???


Yes. Setting a property of the style object to the empty string is
the way to clear it.

The style object/property corresponds to the style attribute in HTML.
Setting a property to the empty string corresponds to that CSS property
not being set in the style attribute string.

Check:
<div id="foo" style="color:red;font-family:sans-serif;">X</div>
<script type="text/javascript">
var elem = document.getElementById("foo");
alert(elem.style.cssText);
elem.style.backgroundColor = "black";
alert(elem.style.cssText);
elem.style.backgroundColor = "";
alert(elem.style.cssText);
</script>

As to whether it is valid ... there is no law against it, and it seems
to work. There is noting in the W3C DOM 2 Style CSS specification about
what happens when you assign an empty string. It doesn't event say that
".style.backgroundColor" is a valid l-value.

There is nothing in the Gecko DOM reference about writing empty
strings to style properties, nor in Microsoft's DHTML reference.

So, cross your fingers. It seems to be a widely accepted practice,
so you can expect browsers to support it for a long time to come.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.