javascript exit function command?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: javascript exit function command?

  1. #1
    Join Date
    Nov 2005
    Posts
    37

    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

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,091

    Re: javascript exit function command?

    "return" lets you exit a function and optionally return a value.

    Example 1:

    PHP Code:
    function test(ab)
    var 
    c=a*b
    return c

    Example 2:

    PHP Code:
    function whatever(a){
    if (
    a<3)
    return


  3. #3
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,870

    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.

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,091

    Re: javascript exit function command?

    Quote 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

  5. #5
    Join Date
    Dec 2005
    Posts
    62

    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>

  6. #6
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,870

    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.

  7. #7
    Join Date
    Jan 2005
    Posts
    399

    Re: javascript exit function command?

    break;
    Online Community Manager

  8. #8
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,091

    Re: javascript exit function command?

    Quote 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 
    while (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=0i<3i++) 

       
    document.write("<BR>" "outer " ":   "); 
       for(
    j=0j<5j++) 
       { 
          
    document.write("inner " " "); 
          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); 


  9. #9
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,870

    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.

  10. #10
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,091

    Re: javascript exit function command?

    Quote 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"

  11. #11
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,870

    Re: javascript exit function command?

    Quote 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.

  12. #12
    Join Date
    Dec 2005
    Posts
    62

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center