Hi!
I have a simple IF/ELSE IF/ELSE block, and it won't work as I hoped:
K_IMG_RED(), K_IMG_BLUE() are function that returns either 1 or 2
(the numbers). I use them as constants.
================================
if( p_iImg == K_IMG_RED() ) /* LINE 0*/
{
oBody.className = 'cssRed' ; /* LINE 1*/
}
else if( p_iImg == K_IMG_BLUE() ) /* LINE 2*/
{
oBody.className = 'cssBlue' ; /* LINE 3*/
}
else
{
oBody.className = 'cssNormal' ; /* LINE 4*/
}
================================
If I try it on IE 5.5, it works (see the complete source
below), but on a Mozilla, the last "else" block is always
executed, no matter the fact that the first (or the second)
"if" condition was true!
Seen in Venkman, debugged line by line, here is the progression (if "p_iImg == 1"
is true):
Line 0 : " p_iImg == K_IMG_RED() " is true.
Line 1 : "oBody.className = 'cssRed' ;" the class of <body> is correctly updated
Line 4 : "oBody.className = 'cssNormal' ;" the class of <body> is updated AGAIN!
I just read, and read again the code, and have shown in to other
developpers, but I found no solution. I keep believing something in the
source just escape my eyes (too much diet coke?), but the "if" blocs seem
quite ok to me...
Below is the complete code (in a simple HTML page), who works
correctly in IE, but not in Mozilla.
Thanks for your help
Raoul Borges
-- To email me, remove the Xs from my email adress
================================================== ======
<html>
<head>
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<script language="javascript" type="text/javascript">
function K_IMG_NORMAL() { return 0 ; }
function K_IMG_RED() { return 1 ; }
function K_IMG_BLUE() { return 2 ; }
function updateHTML(p_iImg)
{
try
{
var oBody = document.getElementById('ID_Body') ;
if( p_iImg == K_IMG_RED() )
{
oBody.className = 'cssRed' ;
}
else if( p_iImg == K_IMG_BLUE() )
{
oBody.className = 'cssBlue' ;
}
else
{
oBody.className = 'cssNormal' ;
}
}
catch(e)
{
throw window.name + '::updateHTML([' + p_iImg + ']):\n{\n' + e + '\n}' ;
}
}
</script>
<style>
..cssNormal
{
background-color = #FFFFFF ;
}
..cssRed
{
background-color = #FF0000 ;
}
..cssBlue
{
background-color = #0000FF ;
}
</style>
</head>
<body id="ID_Body" class="cssNormal">
<a href="javascript: updateHTML(0)">updateHTML(0)</a><br />
<a href="javascript: updateHTML(1)">updateHTML(1)</a><br />
<a href="javascript: updateHTML(2)">updateHTML(2)</a><br />
</body>
</html>
================================================== ======