javascript exit function command?

Hi,

is there a way to exit a procedure/function in javascript?

If i have a button that saves data but i want it to exit if no data has been entered is there a way to say if textBox1 == "" exit function, therefore exiting before the save commands etc are run?

Thanks

C19
[305 byte] By [C19H28O2] at [2007-11-19 19:45:47]
# 1 Re: javascript exit function command?
"return" lets you exit a function and optionally return a value.

Example 1:

function test(a, b)
var c=a*b
return c
}

Example 2:

function whatever(a){
if (a<3)
return
}
HanneSThEGreaT at 2007-11-8 0:22:46 >
# 2 Re: javascript exit function command?
Well, you should not just exit the function. Use an if statement to check if information was entered. If the text is not blank, save.

if(text != ""){save();}
PeejAvery at 2007-11-8 0:23:44 >
# 3 Re: javascript exit function command?
Well, you should not just exit the function. Use an if statement to check if information was entered. If the text is not blank, save.

if(text != ""){save();}
Of course.
I just saw exit function command and then replied :rolleyes:
HanneSThEGreaT at 2007-11-8 0:24:42 >
# 4 Re: javascript exit function command?
You can return a state onsubmit

<script type="text/javascript">
function checkForm(frm){
if(frm.textarea.value != ""){
// do stuff
return true;
}
else{
alert("Please enter some text");
return false;
}
}
</script>
</head>

<body>
<form name="form1" method="post" action="" onsubmit="return checkForm(this)">
<textarea name="textarea"></textarea>
<input type="submit" name="Submit" value="Submit">
</form>
degsy at 2007-11-8 0:25:46 >
# 5 Re: javascript exit function command?
degsy, I'm afraid that you've missed the point. The original intent was to exit a function prematurely not have a return.
PeejAvery at 2007-11-8 0:26:45 >
# 6 Re: javascript exit function command?
break;
JPnyc at 2007-11-8 0:27:41 >
# 7 Re: javascript exit function command?
break;
I'm not sure if I agree with you here. I was always under the impression that the break statement can be used to break out of a while, if, switch, or for statement.

This is how I understand break;
The break statement can be used to terminate a current loop, switch or label statement and pass control to the statement immediately following it.

The following example starts a loop printing the numbers from 1 to 10, buts exits when it reaches 7 with an appropriate message:

var i = 0
while (i < 10)
{
document.write(i);
if (i==7)
{
document.write("the counter has reached " + i);
break;
}
i++;
}

The break statement can also be used with a label as in the following example of two counts, one nested within the other, which will be ended if the inner counter variable is equal to the variable 'x':

outer_loop:
for(i=0; i<3; i++)
{
document.write("<BR>" + "outer " + i + ": ");
for(j=0; j<5; j++)
{
document.write("inner " + j + " ");
if(j==x)
break outer_loop;
}
}

While the break statement on its own can only be used to exit a loop, the optional label can be added to break to exit any kind of statement. This next example tests for an even number and, whenever it finds one, displays it, unless that number is 12:

even_number:
if(i%2==0)
{
if(i==12)
break even_number;
document.write(i);
}
HanneSThEGreaT at 2007-11-8 0:28:45 >
# 8 Re: javascript exit function command?
Break or no break I don't believe this approach is wise. You should not exit if a variable is not set, you should not start unless the variable is set. If you don't understand look at my first post.
PeejAvery at 2007-11-8 0:29:46 >
# 9 Re: javascript exit function command?
Break or no break I don't believe this approach is wise. You should not exit if a variable is not set, you should not start unless the variable is set. If you don't understand look at my first post.
@Paul: I do understand what you're saying, I was just commenting on the usage of break; - all I basically want to establish is "where break; can be used" ;)
HanneSThEGreaT at 2007-11-8 0:30:54 >
# 10 Re: javascript exit function command?
@Paul: I do understand what you're saying, I was just commenting on the usage of break; - all I basically want to establish is "where break; can be used" ;)
I fully understand. I am more concerned with those who do not fully understand what this thread's original problem is about.

Keep up the good work.
PeejAvery at 2007-11-8 0:31:51 >
# 11 Re: javascript exit function command?
For the example given then the return would work fine.
degsy at 2007-11-8 0:32:47 >