By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,469 Members | 1,259 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,469 IT Pros & Developers. It's quick & easy.

How do you stop a javascript program?

P: n/a
While running a program that exceeds the array limits it issues an
alert. I want to then stop the program after filling in the output
boxes with blanks. How do you stop the program?
I have worked on this for days and tried searching the net, but have
found nothing.

Mar 4 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a
I jsut searched groups for stop javascript and found the answer. The
answer is to label loops and then use a break loop statement.

Mar 4 '06 #2

P: n/a
el*********@electrician.com wrote:

[Added attribution and quotation]
el*********@electrician.com wrote:
While running a program that exceeds the array limits it issues an
alert. I want to then stop the program after filling in the output
boxes with blanks. How do you stop the program?
You don't.
I have worked on this for days and tried searching the net, but have
found nothing.


I jsut searched groups for stop javascript and found the answer.
The answer is to label loops and then use a break loop statement.

^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^
No, this reads like utter nonsense. If I understood your OP correctly,
which you did not quote BTW[1], you should use something like

var es = f.elements;
for (var i = 0, len = es.length; i < len; i++)
{
// ... es[i] ...
}

Provided that `f' refers to a HTMLFormElement representing a `form' element
your "output boxes" are controls of. Where you should know that you are
not dealing with an Array object, but with an HTMLCollection object.
PointedEars
_________
[1] <URL:http://safalra.com/special/googlegroupsreply/>
Mar 4 '06 #3

P: n/a
The problem was that I searched an array of conductor ampacities, but
when the end of the array was reached and the right size conductor
could not be found because the derating was too high I needed to stop
the program and alert the user to enter smaller values and fill all the
output form values with blanks and terminate the program. I
accomplished this by setting a flag to True and broke out of the outer
loop with break loop then filled in the blanks by running the
endprogram when the flag is true. It works just fine, now. This is a
rather long program and I have obfuscated it, but if you want to see it
work it is at,
http://electrician.com/calculators/wireocpd_ver_1.html

Mar 5 '06 #4

P: n/a
el*********@electrician.com wrote:
The problem was that I searched an array of conductor ampacities, but
when the end of the array was reached and the right size conductor
could not be found because the derating was too high I needed to stop
the program and alert the user to enter smaller values and fill all the
output form values with blanks and terminate the program. I
accomplished this by setting a flag to True and broke out of the outer
loop with break loop then filled in the blanks by running the
endprogram when the flag is true. It works just fine, now. This is a
rather long program and I have obfuscated it, but if you want to see it
work it is at,
http://electrician.com/calculators/wireocpd_ver_1.html


I will not pretend that I fully understand what you are talking about, since
I am not an electrician, and your explanation lacks specific reference to
the code you are using, and again, to what you are referring to (will you
learn to quote?). However, reviewing your code, it is obvious to me that
it (still) has a number of flaws.

- The underlying HTML code is not Valid. You cannot expect client-side
script code to work on not Valid markup, as you cannot expect a house
to stand on quicksand.

<URL:http://validator.w3.org/check?uri=http://electrician.com/calculators/wireocpd_ver_1.html>

- The script is quite large. For HTTP request efficiency, large resources
should be splitted into smaller chunks. This means the script it should
be moved to an external script resource, e.g. program.js, which should be
included in the HTML document with

<script type="text/javascript" src="program.js"></script>

See also:
<URL:http://www.websiteoptimization.com/services/analyze/wso.php?url=http://electrician.com/calculators/wireocpd_ver_1.html>

- ECMAScript implementations provide a boolean type, with predefined
values of `true' and `false'. It is quite inefficient and error-prone
(unrecognized typos!) to use the string values "true"/"True" and
"false"/"False" instead.

- for (var i = 0; i <= form.aN.length; i++)
{
if (form.aN[i].selected)
{
ay =(form.aN[i].value);
break;
}
}

is syntactically correct, but semantically wrong, as it loops also for
form.aN[form.aN.length], which does not exist because the collection's
index is zero-based. Using

for (var i = 0; i < form.aN.length; i++)

instead, solves this problem. However, it would be still inefficient
(because of repeated deep lookup) and error-prone (because of proprietary
referencing). Therefore, it should be

for (var i = 0, len = form.elements['aN'].options.length; i < len; i++)

Because the reference `form.elements' is used more than one time, it
should be assigned to a local variable once and that variable should
be used instead:

var es = form.elements, o = es['aN'].options;

for (var i = 0, len = o.length; i < len; i++)

But "aN" refers to one single select element that does not have the
"multiple" attribute. Therefore, it should be simply

ay = o[o.selectedIndex].value;

_without_ any unnecessary loop. You will also observe here that the
parentheses in

ay =(form.aN[i].value);

were redundant. Since you have been using obfuscation (which I recommend
against) to make the code as short as possible, you should endeavour not
to include any unnecessary code.

- au = eval(form.S[i].value);

is nonsense. The global eval() method merely evaluates its argument.
Unless its argument is a _string_ value that contains an arithmetic
expression, eval() is entirely redundant, inefficient, and error-prone.

While "S" refers to a `select' element in your HTML code and the `value'
property of HTMLSelectElement objects should not be relied upon (use
selectRef.options[selectRef.selectedIndex] instead), the value of
that property in your case could be either (e.g. for the first item)
"0" or "21-25 (70-77F)".

Assuming it would be "0", being the value of the `value' property of the
respective HTMLOptionElement object as specified in W3C DOM Level 2 HTML,

+es['S'][i]

or even

parseInt(es['S'][i], 10)

would be more efficient.

<URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-59351919>

Assuming that it would be instead "21-25 (70-77F)" as IIRC known from
some DOMs, eval(form.S[i].value) or its standards-compliant reference
equivalent would result in a syntax error.

Therefore the loop should be

for (var i = 0, o = es['S'].options, len = o.length; i < len; i++)

and it should be either

// allows for interpretation of 0x... as hexadecimal value
au = +o[i].value;

or

// parses 0x... as 0
au = parseInt(o[i].value, 10);

However, no loop is necessary at all here, too:

au = parseInt(o[o.selectedIndex].value, 10);

- for (var n = 0; n <= form.aY.length; n++)
{
if (form.aY[n].checked)
{
aB =(form.aY[n].value);
break;
}
}

is different from the above insofar it refers to a group of radiobuttons
with the same name, and not a `select' element. Therefore, the loop is
necessary here, indeed. However,

var aRadio = es['aY'];
for (var n = 0, len = aRadio.length; n < len; n++)
{
if (aRadio[n].checked)
{
aB = aRadio[n].value;
break;
}
}

is still better. (You will observe that the iterator variable and the
`len' variable can be reused. To avoid repeated declaration, you should
declare them locally before the `for' statements only once, and only
[re-]initialize them in each `for' statement. Certainly you do not need
two iterator variables `i' and `n' if the corresponding loops are
independent.)

You should consider using a method (function) that returns the value of
the selected radiobutton in a group. IIRC such a method can be found in
the newsgroup's FAQ, for example.

- As you will probably have guessed after having read the above,

bx =(eval((1.25 *(form.ab.value)))) +(eval(form.aS.value));

is utter nonsense, really.

- If you use boolean values as suggested,

if (ax == "True")

could be simplified to

if (ax)

and

if ((as == "False") &&(G == "False"))

could be simplified to

if (!as && !G)

and so on. (Whereas I recommend to avoid using identifiers starting with
a capital letter if they does not refer to a constructor or a factory;
just to avoid confusion, and to make source code easier readable.)

- ISTM other flaws/errors in your code include merely repetitions of the
flaws/errors that are described above.
HTH

PointedEars
Mar 5 '06 #5

P: n/a
I don't really try to meet the W3.org standards. 98 per cent of the
visitors to my site use IE5.0+. I stopped trying to be compatible with
Netscape, Opera, and Firefox long ago. I used to spend about 30 per
cent of my time on compatibility issues, but now have the attitude that
if the other browsers can't meet MS standards, then too bad. MS is
more of a standard setter than W3.org. Also, most users of my site
have high speed connections so I don't worry about large file sizes. I
have one popular calculator that is 85k right in the web page and I
have not had one single complaint in six years. Besides, js files are
too hard to edit! I suppose I am not in line with academia, but then
I taught myself what I know, and speak from experience. I use
JavaScript because of the ease of use, but more than that, for the
portability. Write it, upload it, and it is available. And as far as
compact code like you suggest, I don't worry about that either. Let
the machines hum and do all the work, they are made for it.
So I am a sloppy programmer, but my site makes money. I would get an F
in your class, but I am not worried about grades any more either. Next
month I start drawing social security.

Mar 5 '06 #6

P: n/a

<el*********@electrician.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
While running a program that exceeds the array limits it issues an
alert. I want to then stop the program after filling in the output
boxes with blanks. How do you stop the program?
I have worked on this for days and tried searching the net, but have
found nothing.


I put the whole thing in a function and do a return to exit.
Mar 5 '06 #7

P: n/a
el*********@electrician.com wrote:
[no quote, lame excuses about error-prone inefficient code]


It is clear to me now that I have wasted my time on you. FOAD.
PointedEars
Mar 5 '06 #8

P: n/a
Perhaps you have learned something from my experience. Let's hope so.
Too many nit pickers are not familiar with real world experiences. I
am a hard knots laborer, electrician, with a degree in Mathematics that
applies production lessons from real work in the electrical trade to
programming. Something that I think is unique and beneficial to
academia types.

Mar 5 '06 #9

P: n/a
> While running a program that exceeds the array limits it issues an
alert. I want to then stop the program after filling in the output
boxes with blanks. How do you stop the program?
I have worked on this for days and tried searching the net, but have
found nothing.


<<I put the whole thing in a function and do a return to exit.>>
Yes. that works, but when the program is very complicated and long it
makes it kind of messy with those brackets about 2000 lines apart. The
program that I have written has taken 10 weeks over the last 10 years
and is very complicated. It applies complex linear programming models
with many exceptions.

Mar 5 '06 #10

P: n/a
wrote on 05 mrt 2006 in comp.lang.javascript:
Perhaps you have learned something from my experience. Let's hope so.
Too many nit pickers are not familiar with real world experiences. I
am a hard knots laborer, electrician, with a degree in Mathematics that
applies production lessons from real work in the electrical trade to
programming. Something that I think is unique and beneficial to
academia types.


The academia types should be grateful.

But, who are you talking to?

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at the
top of the article, then click on the "Reply" at the bottom of the article
headers. <http://www.safalra.com/special/googlegroupsreply/>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Mar 5 '06 #11

P: n/a
Zif
el*********@electrician.com wrote:
I don't really try to meet the W3.org standards. 98 per cent of the
visitors to my site use IE5.0+.
Your decision to use proprietary, largely undocumented 'standards' is a
significant part of the cause of you maintenance problems.

I stopped trying to be compatible with
Netscape, Opera, and Firefox long ago.
That becomes self-fulfilling. If your site doesn't work with those
browsers, then people who use them will stop visiting your site.

I used to spend about 30 per
cent of my time on compatibility issues, but now have the attitude that
if the other browsers can't meet MS standards, then too bad. MS is
more of a standard setter than W3.org.
Your compatibility issues are most likely *because* you use MS as a
'standard'. Using W3C standards, in most cases, reduces
incompatibility. Microsoft is a contributor to and supporter of the W3C
standards you deride.

Also, most users of my site
have high speed connections so I don't worry about large file sizes. I
have one popular calculator that is 85k right in the web page and I
have not had one single complaint in six years. Besides, js files are
too hard to edit!
Your difficulty with editing the files is a direct result of them being
poorly written and your failure to follow even basic design principles,
much less standards.

I suppose I am not in line with academia, but then
I taught myself what I know, and speak from experience. I use
JavaScript because of the ease of use, but more than that, for the
portability. Write it, upload it, and it is available. And as far as
compact code like you suggest, I don't worry about that either. Let
the machines hum and do all the work, they are made for it.
So I am a sloppy programmer, but my site makes money. I would get an F
in your class, but I am not worried about grades any more either. Next
month I start drawing social security.


I'll presume that is because of your age rather than employability -
good for you.

A review of your code shows that there will be many maintenance issues
because of the poor structure of the code, complete disregard for basic
design principles and standards.

Do your qualifications as an electrician also qualify you to write
thousands of lines of production code and publish it as a public
resource with complete disregard for basic design principles and standards?

I'll bet you don't even know if your calculator will provide the correct
result in all cases - how did you test it? Do you have any
understanding of how to test it? If someone uses your calculator and
gets the wrong answer that subsequently causes damage to people or
property, what responsibility are you going to take for that? Do you
have professional indemnity insurance for the programs you write and
publish on the web?

You don't even mention on your site that your calculator will not work
for some browsers - your attitude is beyond irresponsible, it is negligent.
--
Zif
Mar 6 '06 #12

P: n/a
el*********@electrician.com wrote:
Perhaps you have learned something from my experience.
Here's something you can learn from my experience with your code.

You make frequent use of the following function to check number entry
(incidentally, obfuscation makes maintenance even harder for you):

function e(U){
var T = true;
var i = 0;
var aD = U.length;
var ch;
while (i<aD){
ch = U.substring(i, i+1);
if ( ( (ch<"0") || (ch>"9") ) && (ch!=".") ){
alert("Invalid Entry");
T = false;
break;
}
i++;
}
return T;
}
In your 'Continuous Load' field, enter 1.1.1 and your program simply
dies without any error message.

How can this be harmful? Calculate the voltage drop for a conductor and
enter say 50 for the distance from supply (I got zero). Change that to
5000.0. and calculate the value again - guess what? No change to the
voltage drop, yet deleting the last '.' shows a drop of 2.9 (in my case)
- yet a user could happily print out your recommendation from the
previous entry without being told your program screwed up.
10 years in development, 10 minutes to find a bug.
Try putting 50,000 in the field - again, no error message but I'm given
a voltage drop of zero (which I'll guess is erroneous). So even when
you do detect an erroneous entry, you deal with it by giving a totally
wrong answer.

Guess you're just passing on a lesson from the school of hard knocks, right?

Let's hope so.
I hope you did.

Too many nit pickers are not familiar with real world experiences. I
am a hard knots laborer, electrician, with a degree in Mathematics that
applies production lessons from real work in the electrical trade to
programming. Something that I think is unique and beneficial to
academia types.


Too many idiots think that if it 'works' for a limited set of
circumstances it must be OK.

Maybe those academic types really do know something after all.

--
Rob
Mar 6 '06 #13

P: n/a
el*********@electrician.com wrote :
I don't really try to meet the W3.org standards.
99.99% of all electrical appliances on this planet meet either a
national standard/specification or an international
standard/specification. Same thing with electricity traveling across
borders, across countries. Wires implement industry or army standards.
98 per cent of the
visitors to my site use IE5.0+.

Maybe it's because they don't have any other choice. Netscape 7.x, Opera
7+, Firefox 1.x, Safari 1+ and Icab all have the ability to "disguise"
themselves as IE 6. They can change the user agent string which
identifies them as a browser and browser version when visiting a site.
I stopped trying to be compatible with
Netscape, Opera, and Firefox long ago. I used to spend about 30 per
cent of my time on compatibility issues,
Maybe, at that point, it would have been back then useful to visit a
newsgroup like this one to seek help, assistance. The good thing about
W3C web standards is that most of these are supported by Internet
Explorer 6.
DHTML and DOM objects are different issues and not covered by W3C web
standards but then there are more and more convergence and agreements
than divergences.
but now have the attitude that
if the other browsers can't meet MS standards, then too bad.

Your attitude makes your website less accessible, not more accessible to
other browsers, to other platforms, to other web-aware applications, to
other media, etc...
MS is
more of a standard setter than W3.org.
You would not say that if 90% of your visitors were among Safari,
Firefox, Netscape 7.x, Icab users.
Also, most users of my site
have high speed connections so I don't worry about large file sizes.
You have a complacent attitude here. Visitors having problems with your
site (IE-specific or too big or whatever) will not come back:
realistically speaking, what else would you expect them to do?
I
have one popular calculator that is 85k right in the web page and I
have not had one single complaint in six years.

If any of your visitor could read your posts in this thread, then they
would have a confirmation of how useless, pointless writing you a
complaint would have been.

Besides, js files are
too hard to edit!
With an "Homer Simpson" attitude, yes. With an "Lisa Simpson" attitude,
anyone can learn ... by seeking useful assistance in newsgroup such as
this one.
I suppose I am not in line with academia, but then
I taught myself what I know, and speak from experience. I use
JavaScript because of the ease of use, but more than that, for the
portability. Write it, upload it, and it is available. And as far as
compact code like you suggest, I don't worry about that either. Let
the machines hum and do all the work, they are made for it.
A webpage should never assume that the users visiting a site has endless
system resources (CPU, RAM, high-speed connection). There are lots of
web-aware devices and applications which have modest system resources.
Accessibility laws are more and more recognizing this fact.
So I am a sloppy programmer, but my site makes money. I would get an F
in your class, but I am not worried about grades any more either. Next
month I start drawing social security.


Users/Visitors of a site (not us) always have the final word and they
are the ones who can force a site to file a bankrupcy form. That has
been the case during the dot-boom era (1999-2001) on the web.

Gérard
--
remove blah to email me
Mar 6 '06 #14

P: n/a
el*********@electrician.com wrote :
Perhaps you have learned something from my experience. Let's hope so.
Too many nit pickers
Just forget about Thomas Lahn: most of regular posters and experienced
scripters in here don't like his attitude and harsch habits.
are not familiar with real world experiences.
I remember one company doing wires not meeting wire standards and they
were selling those wires for cheap. After a few fires that burn whole
houses, they were discovered.
I
am a hard knots laborer, electrician, with a degree in Mathematics that
applies production lessons from real work in the electrical trade to
programming. Something that I think is unique and beneficial to
academia types.


Your opinion does not make sense and is not realistic even in the field
of maths and electricity to begin with.

Gérard
--
remove blah to email me
Mar 6 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.