473,503 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

javascript cause itself to stop ?



My javascript program has reason to want to stop.

For example, function A has 5 lines, the second of which calls function
B, which has 5 lines, the first of which calls function C.

But function C discovers that something is very wrong so it does an
"alert" saying something like

Sorry, couldn't make the necessary connection

At this point, I want the program to STOP rather than returning to
function B.

What can I put in function C after the "alert" to make it STOP completely ?

It's too cumbersome to have to put a special check into function B to
make sure that function C was able to make the required connection (not
to mention function C having to communicate this to function B), plus
having to then also put similar verification into function A etc etc.

It's much easier if I can have functions A and B *assume* that function
C successfully made the connection, and in turn function C causes a real
STOP if it can't make the connection.

In case you're wondering how global a STOP I'm talking about, since
clearly I don't want the entire web browser to stop, let alone the
entire machine (ha ha), notice what happens when a syntax error is
encountered in the middle of a javascript. The browser manages to STOP
the javascript after displaying the error. It's that kind of a STOP I'm
looking for.

(in fact, I was tempted to implement my STOP in a very ugly way, by
intentionally doing some sort of "eval" of a syntactically bogus string,
but I didn't try this yet)

er*******@rcn.com 2-Mar-2004

Jul 20 '05 #1
8 2016
Lee
Eric Osman said:

My javascript program has reason to want to stop.

For example, function A has 5 lines, the second of which calls function
B, which has 5 lines, the first of which calls function C.

But function C discovers that something is very wrong so it does an
"alert" saying something like

Sorry, couldn't make the necessary connection

At this point, I want the program to STOP rather than returning to
function B.

What can I put in function C after the "alert" to make it STOP completely ?

It's too cumbersome to have to put a special check into function B to
make sure that function C was able to make the required connection (not
to mention function C having to communicate this to function B), plus
having to then also put similar verification into function A etc etc.
That's how professionals generally write code. Function C should return
a status code (possibly just true or false) indicating success.

If you're sure that your users have modern browsers, you could also
look into "throw"-ing an exception, but that could be even more
trouble than returning a status code, if you also plan to "catch"
the exception.
(in fact, I was tempted to implement my STOP in a very ugly way, by
intentionally doing some sort of "eval" of a syntactically bogus string,
but I didn't try this yet)


Note that this would disable all Javascript in the page, preventing them
from just trying again, etc.

I once evaluated commercial code that responded to any error condition
by calling an "abort" function that simply divided by zero. We didn't
buy it.

Jul 20 '05 #2
>>It's too cumbersome to have to put a special check into function B to
make sure that function C was able to make the required connection (not
to mention function C having to communicate this to function B), plus
having to then also put similar verification into function A etc etc.

That's how professionals generally write code. Function C should return
a status code (possibly just true or false) indicating success.

Let me put it another way then.

Suppose function A calls function B calls function C .

In fact, fuction A calls functions D, E, and F, all of which also call C.

In the original version of function C, it returns a string. It was
never believed to ever fail.

Now, in version 2, we realize that there is a condition under which
function C can't return its string , and we do not intend to provide
recovery for this situation. We only want to put up a message that says

Sorry, please try again during business hours

and then stop.

If we have to redesign function C to return a status code, then we have
to return the string elsewhere. Sure, there are ways to do this, like
return an object that contains both a status code and a string.

But look at all the extra stuff we have to do ! We have to recode
functions A, B, D, E, and F .

Such a recode could be considered warranted if we're enhancing the
product to include some sort of recovery .

But in our example, we don't intend to do any kind of recovery , so the
recoding of all those routines would look something like this:

old code:

function B() {
var str = C();
do stuff
}

new code:

function B() {
var obj = C();
var str;
if (obj.status) {
str = obj.str;
do stuff
}
}
What a pain. I really do think this would be an example in which a
"stop" routine (to be called from function C) would be appropriate
instead of requiring all the recoding.

er*******@rcn.com

Jul 20 '05 #3
Eric Osman <er*******@rcn.com> writes:
What a pain. I really do think this would be an example in which a
"stop" routine (to be called from function C) would be appropriate
instead of requiring all the recoding.


That's what exceptions are for! Nobody will add a "stop" function
when they can just do:

throw "Try again during business hours!";

and even add
try {
... B() ... D() ... E() ... F() ....
} catch (error) {
alert(error);
}
around the body of function A.

The browsers that doesn't support "throw" (IE 4, NS 4, or
contemporaries) are unlikely to be extended with a "stop" function
anyway.

/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 20 '05 #4
Lee
Eric Osman said:
It's too cumbersome to have to put a special check into function B to
make sure that function C was able to make the required connection (not
to mention function C having to communicate this to function B), plus
having to then also put similar verification into function A etc etc.

That's how professionals generally write code. Function C should return
a status code (possibly just true or false) indicating success.

If we have to redesign function C to return a status code, then we have
to return the string elsewhere. Sure, there are ways to do this, like
return an object that contains both a status code and a string.


Not at all. On failure, you have it return a special string
that can't be confused with a valid return value, typically
an empty string.

Jul 20 '05 #5


Lee wrote:
Eric Osman said:
It's too cumbersome to have to put a special check into function B to
make sure that function C was able to make the required connection (not
to mention function C having to communicate this to function B), plus
having to then also put similar verification into function A etc etc.
That's how professionals generally write code. Function C should return
a status code (possibly just true or false) indicating success.

If we have to redesign function C to return a status code, then we have
to return the string elsewhere. Sure, there are ways to do this, like
return an object that contains both a status code and a string.

Not at all. On failure, you have it return a special string
that can't be confused with a valid return value, typically
an empty string.


Not sufficient. Routine C could be modified to return an empty string,
but then routine B needs to be taught to check for it, and then routine
B has to be redesigned (we didn't even discuss what routine B was
returning) and then all the calls to routine B have to be redesigned.

In other cases the situation could be even different. For example,
consider a routine C that is supposed to allocate a global array of
100000 elements, something like

for (var i=0; i<100000; i++)
someGlobalArray[i] = ...

This routine C doesn't return anything, and hence all the callers to it
don't check for any return value.

Similarly, in this new example, let's say routine B doesn't return any
value either, so none of the calls to B ever check for one.
Let's say the system is low on memory, so that somewhere during that
"for" loop, javascript can't expand the array.

Presumably, javascript puts up some sort of "out of memory" error box in
this case, and I'd bet javascript knows how to STOP rather than allowing
routine C to continue running.

So I'm merely asking how one would implement a similar kind of STOP to
those that javascript presumably already knows how to do.

Hmmm, is there a "throw" in javascript ? Maybe some condition can be
"thrown" to cause the stop ?

Jul 20 '05 #6
Lee <RE**************@cox.net> wrote in message news:<c2********@drn.newsguy.com>...
Not at all. On failure, you have it return a special string
that can't be confused with a valid return value, typically
an empty string.


Or return null. If returning null already has a special meaning to
existing code, then would I suggest a special string. Of course this
still requires editing existing code, which is just how things work.
There is no way to stop execution of a thread, short of causing a
javascript error. This is a very hacky way of doing it because you
have to cause a specific error and then assume any time that error
occurs, it is because you caused it on purpose. If the error occurs as
a genuine error, it will be thrown away. Check out my code below. The
onerror handler is set right before the error is caused, and then
removed right away. It is possible another thread could cause this
same error within this time, but if we are going to implement a hack,
we should make it least likely as possible that it could malfunction.
This code is IE 5.5+ specific, but is just meant as an example anyway.
Try it out...

alert( "before" );
stop();
alert( "after" );

function stop () {
window.onerror = handleError;
new Number().toExponential( -1 );
}

function handleError ( errorMessage ) {
window.onerror = null;
if ( errorMessage == "The number of fractional digits is out of
range" ) return true;
}
Jul 20 '05 #7
Eric Osman wrote:
Hmmm, is there a "throw" in javascript ? Maybe some condition can be
"thrown" to cause the stop ?


If you can guarantee a javascript version that supports exceptions in
the users environment then go ahead. I write a lot of server-side
"Javascript" so I know I can make such a guarantee, here's a contrived
example (not tested, but you get the idea!):

function DoSomething(intParam1, strParam2) {
try {
if (isNaN(intParam1)) {
throw new Error(-1, "\"intParam1\" is not a number");
}
if ((typeof strParam2) != "string" || strParam2.length == 0) {
throw new Error(-2, "\"strParam2\" is an invalid or null
string");
}

return "example";
}
catch (err) {
throw new Error(err.number, "DoSomething() failed with
parameters intParam1=\"" + intParam1 + "\", strParam2=\"" + strParam2 +
"\". Message=\r\n" + err.description);
}
finally {
ClearUp();
}
}

function Main() {
try {
alert(DoSomething(1, "test"));
}
catch (err) {
alert("Oops, an error occurred:\r\n" + err.description);
}
}

Main();
--
Andrew Urquhart
- FAQ: http://jibbering.com/faq
- Archive: http://groups.google.com/groups?grou...ang.javascript
- Reply: http://www.andrewu.co.uk/about/conta...ject=Re%3A+clj
Jul 20 '05 #8
Lee
Eric Osman said:

Lee wrote:
Eric Osman said:
>It's too cumbersome to have to put a special check into function B to
>make sure that function C was able to make the required connection (not
>to mention function C having to communicate this to function B), plus
>having to then also put similar verification into function A etc etc.
That's how professionals generally write code. Function C should return
a status code (possibly just true or false) indicating success.
If we have to redesign function C to return a status code, then we have
to return the string elsewhere. Sure, there are ways to do this, like
return an object that contains both a status code and a string.

Not at all. On failure, you have it return a special string
that can't be confused with a valid return value, typically
an empty string.


Not sufficient. Routine C could be modified to return an empty string,
but then routine B needs to be taught to check for it, and then routine
B has to be redesigned (we didn't even discuss what routine B was
returning) and then all the calls to routine B have to be redesigned.


Yes. Welcome to professional programming. Note that there may be
some calls to B that don't need to check the status, and that, in
most cases, it should be better for a routine to return an empty
value than to simply fail. If you need to modify your B code to
handle the case of receiving an empty string, you might as well do
that now, rather than 3 months from now when you discover some
other case in which an empty string might be returned.
In other cases the situation could be even different. For example, ....This routine C doesn't return anything, and hence all the callers to it
don't check for any return value.
And so in that case, you return true or false, and modify the
callers that care to check the value. It's too bad that you
didn't realize the need for status checking earlier, but it
shouldn't be such a big deal to modify the calls.

Hmmm, is there a "throw" in javascript ? Maybe some condition can be
"thrown" to cause the stop ?


Throw was one of my suggestions in my original response,
but it's not supported in older browsers, and you'll still
want to modify the calling routines to catch the exception.

Jul 20 '05 #9

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

Similar topics

6
2521
by: Andy Fish | last post by:
Hi, I want to use an anchor tag to invoke some javascript and I've read that it's bad form to use <a href="javascript:foo()"> I've read endless usenet posts and hint sites on the net, they all...
3
1654
by: Jason | last post by:
I have two applications that I wrote that use asp and javascript source files. Recently both of these applications starting acting strange on my test box. Image icons will randomly stop showing...
4
5453
by: houstoncity2004 | last post by:
Hi, I need help to JavaScript. I am new to this, and am trying to the page online ASAP. I have two submit buttons on one form, and each button will have to do different things. I named both...
12
1901
by: Mark Fox | last post by:
Hello, I am attempting to do something very simple. I have a page MainPage.aspx and a popup window Popup.aspx. When users click on the linkbutton in the popup window I am looking to do some...
4
1824
by: Malik Arykov | last post by:
Hello, All! how debug embedded javascript?
4
1275
by: Steve Murphy | last post by:
I'd like to package a navigation bar into a user control. The problem I'm having is getting the rollover buttons to work. Where do I place the Javascript for the image caching and switching for the...
7
2350
by: dotnetprogrammer via DotNetMonster.com | last post by:
I have an NT Service written in c# (.net framwork 2.0). When it hits a certain error condition, I want it to be to cause itself to stop. How can I do this? -- Message posted via...
8
3622
by: chrisdude911 | last post by:
how do i add video into a javascript web page with my own custom buttons?
3
2110
by: Niall | last post by:
The short version: how does one get Javascript to abort a form submission/page load half way through? Long version: I have a page, the guts of which can be summarised as: <script>
0
7202
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,...
0
7086
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
7332
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...
1
6991
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...
0
4673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.