Problem with eval and continue

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

    Problem with eval and continue

    Hi everyone,
    I'm having some weird problem with evaluating the continue statement.
    Within a for loop I'm trying to evaluate a string (generated somewhere
    earlier) which basically has the continue statement in it. IE6 seems to
    have major problems with that as it generates an error "Can't have
    'continue' outside of loop". Does anyone know why and/or have a
    workaround? I haven't tried any other browser since this one is the
    only one available (company policy).
    I have included some code to reproduce this behaviour. The first and
    second if statements of the testeval function behave as expected. The
    third one however produces the mentionned error.

    Thanks in advance for any help.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Test Page</title>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html;
    charset=ISO-8859-1" />
    <style>
    div {border:1px solid red;margin-top:2px;}
    </style>
    <script language="JavaS cript" type="text/javascript">
    function testeval() {
    for (var x=0; x< 6; x++) {
    document.getEle mentById("div" + x).innerHTML = "&nbsp;";
    if (x == 2) continue;
    if (x == 4) eval("alert('ne xt line will produce an error')");
    if (x == 4) eval("continue" );
    document.getEle mentById("div" + x).innerText = x;
    }
    }
    </script>
    </head>
    <body>
    <button onclick="testev al()">Click here</button>
    <div id="div0">&nbsp ;</div>
    <div id="div1">&nbsp ;</div>
    <div id="div2">&nbsp ;</div>
    <div id="div3">&nbsp ;</div>
    <div id="div4">&nbsp ;</div>
    <div id="div5">&nbsp ;</div>
    <div id="div6">&nbsp ;</div>
    </body>
    </html>

  • Lee

    #2
    Re: Problem with eval and continue

    Roebie said:[color=blue]
    >
    >Hi everyone,
    >I'm having some weird problem with evaluating the continue statement.
    >Within a for loop I'm trying to evaluate a string (generated somewhere
    >earlier) which basically has the continue statement in it. IE6 seems to
    >have major problems with that as it generates an error "Can't have
    >'continue' outside of loop".
    >
    >Thanks in advance for any help.[/color]
    [color=blue]
    > for (var x=0; x< 6; x++) {
    > ...
    > if (x == 4) eval("alert('ne xt line will produce an error')");
    > if (x == 4) eval("continue" );[/color]

    The argument to eval() is compiled and executed in an entirely
    separate context, where it has no idea what loop should continue.

    If you find yourself using eval(), you have usually overlooked
    a simpler and more efficient solution.

    Comment

    • Martin Honnen

      #3
      Re: Problem with eval and continue



      Roebie wrote:

      [color=blue]
      > I'm having some weird problem with evaluating the continue statement.
      > Within a for loop I'm trying to evaluate a string (generated somewhere
      > earlier) which basically has the continue statement in it. IE6 seems to
      > have major problems with that as it generates an error "Can't have
      > 'continue' outside of loop". Does anyone know why and/or have a
      > workaround?[/color]
      [color=blue]
      > if (x == 4) eval("continue" );[/color]

      eval treats its string argument as a JavaScript program so it is parsed
      and executed following the normal rules and a program with a single
      continue
      statement gives you an error, whether you have that as an argument of
      eval or simply in a script block.
      So your use or understanding of eval is weird, not the result you get.

      --

      Martin Honnen

      Comment

      • Roebie

        #4
        Re: Problem with eval and continue

        Thanks Lee for your answer. You confirm what I thought was the problem
        here. Indeed using eval often replaces far more better solutions, but
        not in the framework I'm using it in. The code I added was only to let
        others reproduce the behaviour. The real code is far more complicated.
        Now that you have confirmed the way eval works, I will use a different
        approach, which will still have to involve eval, and which will be a
        little more complicated.

        Comment

        • Roebie

          #5
          Re: Problem with eval and continue

          Thanks Martin for your anwser. Neither my use nor my understanding
          proves to be weird. I expected both your and Lee's answer, which just
          confirm my understanding of eval. I was just hoping there was I way of
          getting this use of eval working, but obviously there isn't.

          Comment

          • G Matthew J

            #6
            Re: Problem with eval and continue

            Lee wrote:[color=blue]
            > Roebie said:[color=green]
            > >
            > >Hi everyone,
            > >I'm having some weird problem with evaluating the continue statement.
            > >Within a for loop I'm trying to evaluate a string (generated somewhere
            > >earlier) which basically has the continue statement in it. IE6 seems to
            > >have major problems with that as it generates an error "Can't have
            > >'continue' outside of loop".
            > >
            > >Thanks in advance for any help.[/color]
            >[color=green]
            > > for (var x=0; x< 6; x++) {
            > > ...
            > > if (x == 4) eval("alert('ne xt line will produce an error')");
            > > if (x == 4) eval("continue" );[/color]
            >
            > The argument to eval() is compiled and executed in an entirely
            > separate context, where it has no idea what loop should continue.
            >
            > If you find yourself using eval(), you have usually overlooked
            > a simpler and more efficient solution.[/color]

            Unless of course you are using JSON in which case eval() is the best
            thing since sliced bread ;)

            As for the OP, this won't help in this context, as the response above
            is correct: eval doesn't know from the enclosing loop. However, it is
            possible to eval more than one statement at a time (instead of on two
            lines as above), by using ; to seperate the statements within the
            string argument to eval

            HTH

            Comment

            • Matt Kruse

              #7
              Re: Problem with eval and continue

              Roebie wrote:[color=blue]
              > Now that you have confirmed the way eval works, I will use a different
              > approach, which will still have to involve eval, and which will be a
              > little more complicated.[/color]

              What situations do you think will still have to involve eval?

              You may have a real need. But most likely, if you post your code, someone
              will show you how to accomplish the same task without eval at all.

              --
              Matt Kruse




              Comment

              Working...