Connecting Tech Pros Worldwide Help | Site Map

difference between return true; and return false;

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 20th, 2005, 08:52 AM
w i l l
Guest
 
Posts: n/a
Default difference between return true; and return false;

Why does this work the way it does?
If someone could explain return true, and return false to me I'd
greatly appreciate it.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Form.html</title>
</head>

<body>
<script language="JavaScript">
function form1_onsubmit() {
var returnValue = false;
if (document.form1.txtName.value == "") {
alert("Please Enter your Name");
document.form1.txtName.focus();
}
else {
returnValue = true;
}
return returnValue;
}
</script>

<form action="formhandler.asp" method="post" name="form1" onSubmit="
return form1_onsubmit()">
name: <input type="text" name="txtName">
<input type="submit" value="Go">

</form>

</body>
</html>


w i l l

  #2  
Old July 20th, 2005, 08:52 AM
Gareth Church
Guest
 
Posts: n/a
Default Re: difference between return true; and return false;

"w i l l" <willis_31_40@yahoo.com> wrote in message
news:93hbgvg9o7sdlssthh7qtrq8gp23rk2uft@4ax.com...[color=blue]
> Why does this work the way it does?
> If someone could explain return true, and return false to me I'd
> greatly appreciate it.[/color]

When you return something from a function, that value gets returned to the
caller. For instance, say I had a method to multiply two numbers. Maybe the
code looks like this:

function multiply(num1, num2) {
return num1 * num2;
}
result = multiply(2, 4);

The function multiply will return the result of it's multiplication to
wherever it was called. The right-hand side of the result assignment is
where the function is called, so that is where the result is returned to.
So, in this case (with the parameters 2 and 4), it is the same as writing
result = 8;

When you are using return true or false with markup, you are indicating
whether or not you want the default action to happen after the javascript
has been executed. An example is needed:

<a href="somepage.html" onClick="alert('Hi'); return true;">Click Me</a>

When the link is clicked the javascript code for the onClick will run first,
and we get an alert. We have used return true, so that is saying when we
click OK to remove the alert, we do want to run the markup. So in this case
once we click OK to dismiss the alert, we would then be taken to
somepage.html. If we changed that to return false we would get the alert,
but wouldn't go to somepage.html.

So in your example if the method (form1_onsubmit) returns false, the default
behaviour of the form (action=formhandler.asp) won't run. In other words we
get the alert (thrown from inside the method), and when we click ok nothing
happens. If that method returns true though, the default behaviour will
occur, so the form will be submitted to the asp file.

Gareth


  #3  
Old July 20th, 2005, 08:52 AM
Bagbourne
Guest
 
Posts: n/a
Default Re: difference between return true; and return false;

"w i l l" <willis_31_40@yahoo.com> wrote in message
news:93hbgvg9o7sdlssthh7qtrq8gp23rk2uft@4ax.com...[color=blue]
> Why does this work the way it does?
> If someone could explain return true, and return false to me I'd
> greatly appreciate it.[/color]

In a onxxxxx method where there is usually some form of standard processing
by the browser, it's just a way of specifying whether further standard
processing should take place.

Returning false means no, returning true means yes. So that example aborted
the submit if the user hasn't entered their name.

Nige


  #4  
Old July 20th, 2005, 08:52 AM
w i l l
Guest
 
Posts: n/a
Default Re: difference between return true; and return false;

On Sat, 5 Jul 2003 04:49:02 +1000, "Gareth Church"
<gechurch@bigpond.com.au> wrote:
[color=blue]
>"w i l l" <willis_31_40@yahoo.com> wrote in message
>news:93hbgvg9o7sdlssthh7qtrq8gp23rk2uft@4ax.com.. .[color=green]
>> Why does this work the way it does?
>> If someone could explain return true, and return false to me I'd
>> greatly appreciate it.[/color]
>
>When you return something from a function, that value gets returned to the
>caller. For instance, say I had a method to multiply two numbers. Maybe the
>code looks like this:
>
>function multiply(num1, num2) {
> return num1 * num2;
>}
>result = multiply(2, 4);
>
>The function multiply will return the result of it's multiplication to
>wherever it was called. The right-hand side of the result assignment is
>where the function is called, so that is where the result is returned to.
>So, in this case (with the parameters 2 and 4), it is the same as writing
>result = 8;
>
>When you are using return true or false with markup, you are indicating
>whether or not you want the default action to happen after the javascript
>has been executed. An example is needed:
>
><a href="somepage.html" onClick="alert('Hi'); return true;">Click Me</a>
>
>When the link is clicked the javascript code for the onClick will run first,
>and we get an alert. We have used return true, so that is saying when we
>click OK to remove the alert, we do want to run the markup. So in this case
>once we click OK to dismiss the alert, we would then be taken to
>somepage.html. If we changed that to return false we would get the alert,
>but wouldn't go to somepage.html.
>
>So in your example if the method (form1_onsubmit) returns false, the default
>behaviour of the form (action=formhandler.asp) won't run. In other words we
>get the alert (thrown from inside the method), and when we click ok nothing
>happens. If that method returns true though, the default behaviour will
>occur, so the form will be submitted to the asp file.
>
>Gareth
>[/color]


Excellent explination, Thanks!
  #5  
Old July 20th, 2005, 08:52 AM
Dan Brussee
Guest
 
Posts: n/a
Default Re: difference between return true; and return false;

In article <93hbgvg9o7sdlssthh7qtrq8gp23rk2uft@4ax.com>, willis_31_40
@yahoo.com says...[color=blue]
> Why does this work the way it does?
> If someone could explain return true, and return false to me I'd
> greatly appreciate it.
>[/color]

It's a boolean thing - you wouldnt understand.

Just kidding :)


--

Remove NOT from email address to reply. AntiSpam in action.
  #6  
Old July 20th, 2005, 08:52 AM
Dan Brussee
Guest
 
Posts: n/a
Default Re: difference between return true; and return false;

In article <93hbgvg9o7sdlssthh7qtrq8gp23rk2uft@4ax.com>, willis_31_40
@yahoo.com says...[color=blue]
> Why does this work the way it does?
> If someone could explain return true, and return false to me I'd
> greatly appreciate it.
>[/color]

Or better yet...

function understand() {
return false;
}


--

Remove NOT from email address to reply. AntiSpam in action.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.