Strange behaviour in IF/ELSE blocks...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Raoul Borges

    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.classNam e = 'cssRed' ;" the class of <body> is correctly updated
    Line 4 : "oBody.classNam e = '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="javas cript" type="text/javascript">

    function K_IMG_NORMAL() { return 0 ; }
    function K_IMG_RED() { return 1 ; }
    function K_IMG_BLUE() { return 2 ; }

    function updateHTML(p_iI mg)
    {
    try
    {
    var oBody = document.getEle mentById('ID_Bo dy') ;

    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="cssNorma l">

    <a href="javascrip t: updateHTML(0)"> updateHTML(0)</a><br />
    <a href="javascrip t: updateHTML(1)"> updateHTML(1)</a><br />
    <a href="javascrip t: updateHTML(2)"> updateHTML(2)</a><br />

    </body>
    </html>

    =============== =============== =============== ===========


  • Martin Honnen

    #2
    Re: Strange behaviour in IF/ELSE blocks...



    Raoul Borges wrote:

    [color=blue]
    > <style>
    > .cssNormal
    > {
    > background-color = #FFFFFF ;[/color]

    That is not CSS you need
    background-color: #FFFFF;
    the same for the other rules below.
    [color=blue]
    > }
    >
    > .cssRed
    > {
    > background-color = #FF0000 ;
    > }
    >
    > .cssBlue
    > {
    > background-color = #0000FF ;
    > }
    > </style>[/color]


    --

    Martin Honnen


    Comment

    • Raoul Borges

      #3
      Re: Strange behaviour in IF/ELSE blocks...

      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.


      Comment

      • kaeli

        #4
        Re: Strange behaviour in IF/ELSE blocks...

        In article <2lffifFbr77iU1 @uni-berlin.de>, paeXrceXbal@hoX tmaXil.com
        enlightened us with...[color=blue]
        > Hi!
        >
        >
        > If I try it on IE 5.5, it works[/color]

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

        (see the complete source[color=blue]
        > below), but on a Mozilla, the last "else" block is always
        > executed,[/color]

        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="javas cript" type="text/javascript">

        function K_IMG_NORMAL() { return 0 ; }
        function K_IMG_RED() { return 1 ; }
        function K_IMG_BLUE() { return 2 ; }

        function updateHTML(p_iI mg)
        {
        try
        {
        var oBody = document.getEle mentById('ID_Bo dy') ;

        if( p_iImg == K_IMG_RED() )
        {
        oBody.className = 'cssRed' ;
        alert(oBody.cla ssName);

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

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

        }
        }
        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="cssNorma l">

        <a href="javascrip t: updateHTML(0)"> updateHTML(0)</a><br>
        <a href="javascrip t: updateHTML(1)"> updateHTML(1)</a><br>
        <a href="javascrip t: updateHTML(2)"> updateHTML(2)</a><br>

        </body>
        </html>

        --
        --
        ~kaeli~
        Support your local medical examiner: die strangely!



        Comment

        • kaeli

          #5
          Re: Strange behaviour in IF/ELSE blocks...

          In article <MPG.1b5c4e5be9 971a9c989f75@nn tp.lucent.com>,
          tiny_one@NOSPAM .comcast.net enlightened us with...[color=blue]
          > In article <2lffifFbr77iU1 @uni-berlin.de>, paeXrceXbal@hoX tmaXil.com
          > enlightened us with...[color=green]
          > > Hi!
          > >
          > >
          > > If I try it on IE 5.5, it works[/color]
          >
          > Not with the right doctype, it doesn't. You're doing something that uses
          > quirks mode.
          >[/color]

          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="javas cript" type="text/javascript">

          function K_IMG_NORMAL() { return 0 ; }
          function K_IMG_RED() { return 1 ; }
          function K_IMG_BLUE() { return 2 ; }

          function updateHTML(p_iI mg)
          {
          try
          {
          var oBody = document.getEle mentById('ID_Bo dy') ;

          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="cssNorma l">

          <a href="javascrip t: updateHTML(0)"> updateHTML(0)</a><br>
          <a href="javascrip t: updateHTML(1)"> updateHTML(1)</a><br>
          <a href="javascrip t: updateHTML(2)"> updateHTML(2)</a><br>

          </body>
          </html>

          --
          --
          ~kaeli~
          What, me, normal?



          Comment

          • Raoul Borges

            #6
            Re: Strange behaviour in IF/ELSE blocks...

            Thanks, M. Honnen and Kaeli, for your answers.
            The correction of the CSS mistake made it work!
            [color=blue][color=green]
            > > <style>
            > > .cssNormal
            > > {
            > > background-color = #FFFFFF ;[/color]
            >
            > That is not CSS you need
            > background-color: #FFFFF;[/color]

            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_iI mg=integer:2) in <file:/C:/hvs/__divers/test_mozilla/test_if_else/test_if_else.ht ml> line 13

            011: function updateHTML(p_iI mg)

            012: {

            013: try

            014: {

            015: var oBody = document.getEle mentById('ID_Bo dy') ;

            Continuing from breakpoint.

            #0: function updateHTML(p_iI mg=integer:2) in <file:/C:/hvs/__divers/test_mozilla/test_if_else/test_if_else.ht ml> line 15

            015: var oBody = document.getEle mentById('ID_Bo dy') ;

            #0: function updateHTML(p_iI mg=integer:2) in <file:/C:/hvs/__divers/test_mozilla/test_if_else/test_if_else.ht ml> line 17

            017: if( p_iImg == K_IMG_RED() )

            #0: function updateHTML(p_iI mg=integer:2) in <file:/C:/hvs/__divers/test_mozilla/test_if_else/test_if_else.ht ml> line 21

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

            #0: function updateHTML(p_iI mg=integer:2) in <file:/C:/hvs/__divers/test_mozilla/test_if_else/test_if_else.ht ml> line 23

            023: oBody.className = 'cssBlue' ;

            #0: function updateHTML(p_iI mg=integer:2) in <file:/C:/hvs/__divers/test_mozilla/test_if_else/test_if_else.ht ml> line 27

            027: oBody.className = 'cssNormal' ;

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

            =============== =============== =============== ==


            Comment

            • Dr John Stockton

              #7
              Re: Strange behaviour in IF/ELSE blocks...

              JRS: In article <2lffifFbr77iU1 @uni-berlin.de>, seen in
              news:comp.lang. javascript, Raoul Borges <paeXrceXbal@ho XtmaXil.com>
              posted at Mon, 12 Jul 2004 14:46:27 :[color=blue]
              >
              >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.[/color]

              [color=blue]
              > 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*/
              > }[/color]


              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.demo n.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)

              Comment

              • Lasse Reichstein Nielsen

                #8
                Re: Strange behaviour in IF/ELSE blocks...

                Dr John Stockton <spam@merlyn.de mon.co.uk> writes:
                [color=blue]
                > 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' ;
                > }[/color]

                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 - lrn@hotpop.com
                DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                'Faith without judgement merely degrades the spirit divine.'

                Comment

                • Dr John Stockton

                  #9
                  Re: Strange behaviour in IF/ELSE blocks...

                  JRS: In article <n024u37s.fsf@h otpop.com>, seen in
                  news:comp.lang. javascript, Lasse Reichstein Nielsen <lrn@hotpop.com >
                  posted at Tue, 13 Jul 2004 03:04:55 :[color=blue]
                  >Dr John Stockton <spam@merlyn.de mon.co.uk> writes:
                  >[color=green]
                  >> 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' ;
                  >> }[/color]
                  >
                  >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' ;
                  > }[/color]

                  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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                  <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                  Comment

                  Working...