javascriptcsscss-expressions

One-time CSS expressions


This CSS expression

P {
   background-color: expression((new Date()).getHours()%2 ? "#B8D4FF" : "#F08A00");
} 

can be called thousands of times

but this approach, means

<style>
P {
   background-color:expression(altBgColor(this));
}
</style>
<script type="text/javascript">
function altBgColor(elem) {
    elem.style.background = new Date()).getHours()%2 ? "#B8D4FF" : "#F08A00";
}
</script>

means it won't. Why?


Solution

  • What did you expect to happen?

    The very first time your dynamic expression is called it overwrites the background property which contained that expression.