-
March 17th, 2006, 04:40 AM
#1
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
-
March 17th, 2006, 06:10 AM
#2
Re: javascript exit function command?
"return" lets you exit a function and optionally return a value.
Example 1:
PHP Code:
function test(a, b)
var c=a*b
return c
}
Example 2:
PHP Code:
function whatever(a){
if (a<3)
return
}
-
March 17th, 2006, 08:16 AM
#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.
Code:
if(text != ""){save();}
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
March 17th, 2006, 08:22 AM
#4
Re: javascript exit function command?
Originally Posted by peejavery
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.
Code:
if(text != ""){save();}
Of course.
I just saw exit function command and then replied
-
March 24th, 2006, 08:37 AM
#5
Re: javascript exit function command?
You can return a state onsubmit
Code:
<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>
-
March 24th, 2006, 08:43 AM
#6
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.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
March 24th, 2006, 09:42 AM
#7
Re: javascript exit function command?
Online Community Manager
-
March 25th, 2006, 02:20 AM
#8
Re: javascript exit function command?
Originally Posted by JPnyc
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:
PHP Code:
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':
PHP Code:
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:
PHP Code:
even_number:
if(i%2==0)
{
if(i==12)
break even_number;
document.write(i);
}
-
March 25th, 2006, 02:49 AM
#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.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
March 25th, 2006, 03:11 AM
#10
Re: javascript exit function command?
Originally Posted by peejavery
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"
-
March 25th, 2006, 04:57 AM
#11
Re: javascript exit function command?
Originally Posted by HanneSThEGreaT
@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.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
April 5th, 2006, 05:51 AM
#12
Re: javascript exit function command?
For the example given then the return would work fine.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|