One-time CSS expressions
-
12-11-2019 - |
Question
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.
OTHER TIPS
Change <style>
to <script>
<script type="text/javascript">
function altBgColor(elem) {
elem.style.background = new Date().getHours()%2 ? "#B8D4FF" : "#F08A00";
}
</script>
"For scripting, a dynamic property can be any legal JScript or Microsoft Visual Basic Scripting Edition (VBScript) statement." says MSDN. JScript is NOT JavaScript. Also, a good thing is trying to stay away from CSS expressions altogether :)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow