473,397 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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
Jul 20 '05 #1
5 107253
"w i l l" <wi**********@yahoo.com> wrote in message
news:93********************************@4ax.com...
Why does this work the way it does?
If someone could explain return true, and return false to me I'd
greatly appreciate it.


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
Jul 20 '05 #2
"w i l l" <wi**********@yahoo.com> wrote in message
news:93********************************@4ax.com...
Why does this work the way it does?
If someone could explain return true, and return false to me I'd
greatly appreciate it.


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
Jul 20 '05 #3
On Sat, 5 Jul 2003 04:49:02 +1000, "Gareth Church"
<ge******@bigpond.com.au> wrote:
"w i l l" <wi**********@yahoo.com> wrote in message
news:93********************************@4ax.com.. .
Why does this work the way it does?
If someone could explain return true, and return false to me I'd
greatly appreciate it.


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

Excellent explination, Thanks!
Jul 20 '05 #4
In article <93********************************@4ax.com>, willis_31_40
@yahoo.com says...
Why does this work the way it does?
If someone could explain return true, and return false to me I'd
greatly appreciate it.


It's a boolean thing - you wouldnt understand.

Just kidding :)
--

Remove NOT from email address to reply. AntiSpam in action.
Jul 20 '05 #5
In article <93********************************@4ax.com>, willis_31_40
@yahoo.com says...
Why does this work the way it does?
If someone could explain return true, and return false to me I'd
greatly appreciate it.


Or better yet...

function understand() {
return false;
}
--

Remove NOT from email address to reply. AntiSpam in action.
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: drs | last post by:
I just upgraded my Python install, and for the first time have True and False rather than 1 and 0. I was playing around at the command line to test how they work (for instance, "if 9:" and "if...
1
by: rkapur12 | last post by:
In my jsp code I have an image which if someone clicks on that it stores data like "string1" in the hidden field that I have defined on page as: <html:hidden property="selectedOfficeType"...
4
by: Wayne Wengert | last post by:
I am using VB in a VSNET 2003 Windows application. I've run into a situation where, when trying to set a bit value in a SQL Server 2000 database I get errors because the values extracted from a...
2
by: xllx.relient.xllx | last post by:
Hi, there. I need two simple things explained to me: 1.) I want to know why the "true" overloaded operator method gets called with a test to a null-initialized instance: public class...
3
by: comp.lang.php | last post by:
class ThumbView extends PaginationView { function ThumbView() {} /** * Determine if the original image is actually an image and thus a thumbnail can be generated at all * * @access public...
7
by: moltendorf | last post by:
Okay... so I'm generating a custom Slideshow tool for my friend... and I never seem to get any replies from Webberdev, so here I am, posting on a completely new forum... Anyways, the code is here:...
4
by: cssExp | last post by:
hello, i want to check for errors on submitting a form, on encountering an error it runs a function and returns a false preventing form submit. I have implemented that. function eCheck() { ...
3
by: MKO | last post by:
Hi Basically, this is the last function called and checks if there were any errors, if so it returns all the errors in one alert message, if not form should proceed on submit. So if there is an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.