473,548 Members | 2,696 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.htm l</title>
</head>

<body>
<script language="JavaS cript">
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="formhan dler.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 107275
"w i l l" <wi**********@y ahoo.com> wrote in message
news:93******** *************** *********@4ax.c om...
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=formhan dler.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**********@y ahoo.com> wrote in message
news:93******** *************** *********@4ax.c om...
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******@bigpo nd.com.au> wrote:
"w i l l" <wi**********@y ahoo.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.htm l. 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=formhan dler.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
2551
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 True:" both lead to the conditional being executed, but True == 9 -> False, that this would be true was not obvious to me -- "True is True" is True,...
1
4212
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" value="" /> later when I click save button (with the following code:) <input id="saveButtonId" type="button" value="Save"...
4
7199
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 datarow return True or False. In the snippet below, the SQL becomes "Update myTable SET EQ = True" which fails with an error that "True" is not a valid...
2
1933
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 AnimalClass { public static bool operator true( AnimalClass lhs ) { Console.WriteLine("In true operator");
3
1634
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 * @param mixed $image_name (optional)
7
1646
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: http://moltx.name/index_code.php?f=../../extras/help/sites/darkvirus/slideshow.php&u=1183295841 The page is here:...
4
3676
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() { var filePath = document.getElementsByName("fileSelect"); if (filePath == '')
3
2044
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 alert(msg) returned, I don't want the form to submit. Any suggestions on how to add this would be appreciated.
0
7518
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7444
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7467
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6039
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5367
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5085
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3497
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1932
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1054
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.