473,387 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Strange behaviour in IF/ELSE blocks...

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>

================================================== ======
Jul 23 '05 #1
8 1262


Raoul Borges wrote:

<style>
.cssNormal
{
background-color = #FFFFFF ;
That is not CSS you need
background-color: #FFFFF;
the same for the other rules below.
}

.cssRed
{
background-color = #FF0000 ;
}

.cssBlue
{
background-color = #0000FF ;
}
</style>

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 23 '05 #2
By the way, it was tested on IE 5.5 and Mozilla 1.7
-- Raoul Borges
To email me, remove the Xs from my email adress.
Jul 23 '05 #3
In article <2l************@uni-berlin.de>, pa*********@hoXtmaXil.com
enlightened us with...
Hi!
If I try it on IE 5.5, it works
Not with the right doctype, it doesn't. You're doing something that uses
quirks mode.

(see the complete source below), but on a Mozilla, the last "else" block is always
executed,


No it isn't. Put an alert in there.

You get the same results in IE6 and Mozilla (well, Netscape 7.1) with
this code. Not that it works. It doesn't. But it might help you narrow
down the problem.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<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' ;
alert(oBody.className);

}
else if( p_iImg == K_IMG_BLUE() )
{
oBody.className = 'cssBlue' ;
alert(oBody.className);

}
else
{
oBody.className = 'cssNormal' ;
alert(oBody.className);

}
}
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>

--
--
~kaeli~
Support your local medical examiner: die strangely!
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #4
In article <MP************************@nntp.lucent.com>,
ti******@NOSPAM.comcast.net enlightened us with...
In article <2l************@uni-berlin.de>, pa*********@hoXtmaXil.com
enlightened us with...
Hi!
If I try it on IE 5.5, it works


Not with the right doctype, it doesn't. You're doing something that uses
quirks mode.


Using Martin's suggestion, which I can't believe I didn't catch (bad
me), it worked in IE6 and NN7.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<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>

--
--
~kaeli~
What, me, normal?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #5
Thanks, M. Honnen and Kaeli, for your answers.
The correction of the CSS mistake made it work!
<style>
.cssNormal
{
background-color = #FFFFFF ;


That is not CSS you need
background-color: #FFFFF;


But there remains one curiosity:

I tried again playing with Venkman, and I found that, with the same page,
with the CSS correction, when debugging step by step Venkman continues
to enter in the last "else" block, as show in the Venkman log below:

The line:

021: else if( p_iImg == K_IMG_BLUE() )

is "true", and as such, we enter the block:

023: oBody.className = 'cssBlue' ;

and then, instead of quiting the function, we continue to

027: oBody.className = 'cssNormal' ;

which can be quite confusing, as this instruction IS NOT executed
(as show by the deep blue screen of the example).

(Going through the source in debug, step by step, led me to think
something was wrong with my if/else instead of searching elsewhere)
Ok, anyway, thanks again!
-- Raoul Borges
To email me, remove the Xs from my email adress
===============================================

Stopped for breakpoint.

#0: function updateHTML(p_iImg=integer:2) in <file:/C:/hvs/__divers/test_mozilla/test_if_else/test_if_else.html> line 13

011: function updateHTML(p_iImg)

012: {

013: try

014: {

015: var oBody = document.getElementById('ID_Body') ;

Continuing from breakpoint.

#0: function updateHTML(p_iImg=integer:2) in <file:/C:/hvs/__divers/test_mozilla/test_if_else/test_if_else.html> line 15

015: var oBody = document.getElementById('ID_Body') ;

#0: function updateHTML(p_iImg=integer:2) in <file:/C:/hvs/__divers/test_mozilla/test_if_else/test_if_else.html> line 17

017: if( p_iImg == K_IMG_RED() )

#0: function updateHTML(p_iImg=integer:2) in <file:/C:/hvs/__divers/test_mozilla/test_if_else/test_if_else.html> line 21

021: else if( p_iImg == K_IMG_BLUE() )

#0: function updateHTML(p_iImg=integer:2) in <file:/C:/hvs/__divers/test_mozilla/test_if_else/test_if_else.html> line 23

023: oBody.className = 'cssBlue' ;

#0: function updateHTML(p_iImg=integer:2) in <file:/C:/hvs/__divers/test_mozilla/test_if_else/test_if_else.html> line 27

027: oBody.className = 'cssNormal' ;

#0: function (null)() in <javascript: updateHTML(2)> line 1

===============================================
Jul 23 '05 #6
JRS: In article <2l************@uni-berlin.de>, seen in
news:comp.lang.javascript, Raoul Borges <pa*********@hoXtmaXil.com>
posted at Mon, 12 Jul 2004 14:46:27 :

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 all else fails, you could try

switch(true) {
case p_iImg == K_IMG_RED() : oBody.className = 'cssRed' ; break ;
case p_iImg == K_IMG_BLUE() : oBody.className = 'cssBlue' ; break ;
default : oBody.className = 'cssNormal' ;
}

or

oBody.className =
p_iImg == K_IMG_RED() ? 'cssRed' :
p_iImg == K_IMG_BLUE() ? 'cssBlue' : 'cssNormal' ;

Untested; but should have the same effect IIRC.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
Jul 23 '05 #7
Dr John Stockton <sp**@merlyn.demon.co.uk> writes:
If all else fails, you could try

switch(true) {
case p_iImg == K_IMG_RED() : oBody.className = 'cssRed' ; break ;
case p_iImg == K_IMG_BLUE() : oBody.className = 'cssBlue' ; break ;
default : oBody.className = 'cssNormal' ;
}


That looks like something that can be simplified to:
---
switch(p_iImg) {
case K_IMG_RED() : oBody.className = 'cssRed' ; break ;
case K_IMG_BLUE() : oBody.className = 'cssBlue' ; break ;
default : oBody.className = 'cssNormal' ;
}
---
/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 23 '05 #8
JRS: In article <n0**********@hotpop.com>, seen in
news:comp.lang.javascript, Lasse Reichstein Nielsen <lr*@hotpop.com>
posted at Tue, 13 Jul 2004 03:04:55 :
Dr John Stockton <sp**@merlyn.demon.co.uk> writes:
If all else fails, you could try

switch(true) {
case p_iImg == K_IMG_RED() : oBody.className = 'cssRed' ; break ;
case p_iImg == K_IMG_BLUE() : oBody.className = 'cssBlue' ; break ;
default : oBody.className = 'cssNormal' ;
}


That looks like something that can be simplified to:
---
switch(p_iImg) {
case K_IMG_RED() : oBody.className = 'cssRed' ; break ;
case K_IMG_BLUE() : oBody.className = 'cssBlue' ; break ;
default : oBody.className = 'cssNormal' ;
}


Could and should; it was modified from something else, which could not.

It might be possible to arrange for the items after "case" to be
unchanging variables

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: CHRISTOF WARLICH | last post by:
Hi, the following few lines of code are showing a quite strange and unexpected behaviour of namespaces that makes me worry wheater I should rely on namespaces in the future at all. The...
20
by: Markus Sandheide | last post by:
Hello! Execute these lines: int x = 1; x = x > 2345678901; You will get: x == 1 with Borland C++ Builder
2
by: Paul Drummond | last post by:
Hi all, I am developing software for Linux Redhat9 and I have noticed some very strange behaviour when throwing exceptions within a shared library. All our exceptions are derived from...
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
17
by: SW1 | last post by:
I wrote a small program which does something like tftp - transfering files and some chat, anyway i got a problem with fwrite, here is a snippet of my code: while(length > 0) { putchar('.');...
4
by: ignw82 | last post by:
Hi all, I have a strange behaviour in dataview, maybe you can help me. the behaviour is like this : First I made a datatable (odt) in data set, and then I created a dataview using this...
5
by: Ian | last post by:
Hi everyone, I have found some bizarre (to me...!) behaviour of the Form_Activate function. I have a form which has a button control used to close the form and a subform with a datasheet view...
8
by: Dox33 | last post by:
I ran into a very strange behaviour of raw_input(). I hope somebody can tell me how to fix this. (Or is this a problem in the python source?) I will explain the problem by using 3 examples....
3
by: RedWiz | last post by:
Hi i have to develop a multihreaded php application on linux, then through pcntl_fork and wait. I tried it, but there something going wrong, i think. The difference whit other languages like c...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.