473,748 Members | 9,933 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>

Aug 23 '05 #1
6 2118
Lee
Roebie said:

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. for (var x=0; x< 6; x++) {
...
if (x == 4) eval("alert('ne xt line will produce an error')");
if (x == 4) eval("continue" );


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.

Aug 23 '05 #2


Roebie wrote:

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? if (x == 4) eval("continue" );


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
http://JavaScript.FAQTs.com/
Aug 23 '05 #3
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.

Aug 23 '05 #4
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.

Aug 23 '05 #5
Lee wrote:
Roebie said:

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.

for (var x=0; x< 6; x++) {
...
if (x == 4) eval("alert('ne xt line will produce an error')");
if (x == 4) eval("continue" );


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.


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

Aug 23 '05 #6
Roebie wrote:
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.


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
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Aug 23 '05 #7

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

Similar topics

4
3844
by: JesusFreak | last post by:
From: us_traveller@yahoo.com (JesusFreak) Newsgroups: microsoft.public.scripting.jscript Subject: toolbar script problem NNTP-Posting-Host: 192.92.126.136 Recently, I downloaded the following beautiful script "http://javascript.internet.com/navigation/toolbar-menu.html". It works like a charm. I made my webpage in frames, where the nav-frame shows the menubar, so whenever I click a link in the menubar, it opens in the frame below. But...
13
1617
by: TJS | last post by:
how can I reference an element name if it has a colon in it ? example that creates error : =========================== objForm._ctl1:password.required = 1;
8
13046
by: werner | last post by:
Hi! I don't want to use eval() in order to parse a user-supplied formula. What alternatives do I have? PHP has no standard functionality for tokenizing or parsing expressions in this regard. Here is a simple example: The user supplies the following formula in string format, "a = (6+10)/4", and the script needs to find out what the value of 'a' is.
15
3665
by: manstey | last post by:
Hi, I have a text file called a.txt: # comments I read it using this:
4
2789
by: Jm lists | last post by:
Hello members, I want to know does the "eval" in python have the same features as in Perl (capture errors)? For example,in perl I can wrote: $re = eval { 1 / 0 }; Though 1/0 is a fatal error but since it's in "eval" block so the perl
7
4827
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the expandable row, the hidden row is made visible with css. The problem is when i sort the rows, the hidden rows get sorted as well which i don't want and want to be moved (while sorting) relative to their parent rows. The following is my complete html code...
0
2670
by: Ofelia | last post by:
Hi, I'm new to this forum and to Perl language but I would like to ask for your help. I'm working in Linux and the files I need to process are in the format “file.gz”. I created a script which should decompress, open and then delete nearly 400 files. To do so I use "open FILEPT, "zcat $filename|"". In the beginning the script works fine, but after about 300 files processed I get an error on Open function: “proc: Could not open file...
10
1221
by: TheSaint | last post by:
Hi, It seems to be strange that give me syntax error inside an eval statement. I'm looking at it carefully but I can't see any flaw. Here it's part of the code: for nn in stn_items: value= eval('cp.%s' %nn) if value and (nn in 'log, trash, multithread, verbose, download'):
1
1848
nitindel
by: nitindel | last post by:
Hi All, Please tell me any good site for Gridview control.(not for datagrid). I am facing error in fetching the values of the Bound columns in the gridview: lease tell me how should i fetch the value..of a bound column..?? Below is the code.:
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8831
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9325
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9249
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8244
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.