Solved

Halting execution of Javascript code

Posted on 2000-03-23
9
509 Views
Last Modified: 2013-12-18
Is there a way that I can halt the execution of my Javascript code if a certain condition is met? I call a series of functions, one after another, and if one function returns a certain value I do not want to continue with the next function. I realize I could probably set a variable and check this variable in each function but it seems like a lot of code. Is there some sort of a STOP or END or EXIT command?
0
Comment
Question by:snocross
  • 4
  • 3
  • 2
9 Comments
 
LVL 6

Expert Comment

by:ghassan99
ID: 2649671
Just use 'Return' to exit the function.

-Gus

0
 
LVL 5

Author Comment

by:snocross
ID: 2649783
Gus, I don't think that will work for my situation becuase I also want to stop executing ALL of the javascript, not just exit the one function.  For example if I call several functions when I press a SUBMIT button then if the first function has a problem I don't want the second function to evaluate.  

Example:

function saveSubmit() {
brokerValidate();
customerValidate();
otherValidate(); ....etc }

function brokerValidate() {if (document.forms[0].Broker.value == "") {alert("Please enter a broker number.");
THIS IS WHERE I WANT TO STOP, OTHERWISE THEY GET PROMPTED FOR EVERY FIELD THEY LEFT BLANK, ALL AT ONCE!}      

I realize I could put all the validation in one function and use IF, ELSE IF,  but the problem is not only do I check the validation when the form is submitted but also each time they tab out of a field so I need each validation to be a separate function.


0
 
LVL 5

Author Comment

by:snocross
ID: 2649817
Gus, I don't think that will work for my situation becuase I also want to stop executing ALL of the javascript, not just exit the one function.  For example if I call several functions when I press a SUBMIT button then if the first function has a problem I don't want the second function to evaluate.  

Example:

function saveSubmit() {
brokerValidate();
customerValidate();
otherValidate(); ....etc }

function brokerValidate() {if (document.forms[0].Broker.value == "") {alert("Please enter a broker number.");
THIS IS WHERE I WANT TO STOP, OTHERWISE THEY GET PROMPTED FOR EVERY FIELD THEY LEFT BLANK, ALL AT ONCE!}      

I realize I could put all the validation in one function and use IF, ELSE IF,  but the problem is not only do I check the validation when the form is submitted but also each time they tab out of a field so I need each validation to be a separate function.


0
 
LVL 1

Accepted Solution

by:
bennyliaw earned 50 total points
ID: 2649818
Change the function such that it will return either a boolean flag to indicate whether you want to halt the process or continue.

So even if you have nested functions, one after another, you can halt all functions at once because the true flag will be propagated to the outer functions. Not much code to be added, just check the return flag at each function call.

eg.
function F1
{
   // your code ....
   if F2() { return true };
   // more code ....
   return false;
}

function F2
{
   // your code ....
   if F3() { return true };
   // more code ....
   return false;
}

function F3
{
   if (halt condition) { return true }


   return false;
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Author Comment

by:snocross
ID: 2649871
Bennyliaw,

Correct me if I'm wrong but it looks like your example would process all of the functions until it found the first error.  This is great but I also call the functions one at a time.  If I'm tabbing out of the CUSTOMER field I just want to validate the CUSTOMER field... I don't want to check every field at this point, not until I press the SUBMIT button at which point I think your code would work great.  Basicly I need to do two things, first validate each field one function at a time as I exit each field, and secondly I need to validate the entire form (all validation functions) when the form is submitted.
0
 
LVL 6

Expert Comment

by:ghassan99
ID: 2650890
Then why dont u just put the function call of each field in the onBlur event of that field?  Or is there some fields that rely on the values of other fields?

-Gus
0
 
LVL 6

Expert Comment

by:ghassan99
ID: 2651570
Sorry disregard my last comment as I didnt realize that u added a comment to Benny's comment.

-Gus
0
 
LVL 1

Expert Comment

by:bennyliaw
ID: 2654130
Snocross,

Sorry for the late respond, I did not see your reply to my comment when I submitted, probably because of the slow internet access here.

I believe Gus' way is the only way to validate the field when the user tab out of the field.

benny
0
 
LVL 5

Author Comment

by:snocross
ID: 2705281
I didn't quite understand your syntax (ie: if F2(){Return code} ) but I ended up setting a flag like you said and it worked.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Email Message Background Color 4 125
Send a link through lotus notes automated mail 11 134
VBA Lotus notes 2 195
Lotus Notes - Open specific view in frameset (right side) 2 228
For users on the Lotus Notes 8 Standard client, this article provides information on checking the Java Heap size and adjusting it to half of your system RAM in attempt to get the Lotus Notes 8.x Standard client to run faster.  I've had to exercise t…
For beginners of Lotus Notes user this is important to know about the types of files and their location supported by IBM Notes. Mostly users are unaware about how many file types are created and what their usages are. This Article is fully dedicated…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now