473,799 Members | 3,080 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

'Lost' function return value: what am I doing wrong?

Hi.
I am relatively new to js, but I did think I was starting to get the
hang of it. Then this happened...

I have a form with an onsubmit event handler:
<form id="uploadForm " method="post" action="..."
onSubmit="check Dates()">
The event handler does some minor validation, then returns true or
false:
function checkDates(y, m, d) {
snip<<

if (endDate.getTim e() >= startDate.getTi me())
return true;

alert("Start date must precede end date");
return false;
}

(The arguments to the function are used when it is called elsewhere,
not as onsubmit.)

I also have a library class which needs to process the form submit, so
it hooks onsubmit like this:
MyClass.setOnSu bmit = function(listId ) {
var list = document.getEle mentById(listId );
var form = list.form;

var f = form.onsubmit;
if (typeof f == "function") {
form.oldOnSubmi t = f;
form.onsubmit = function(){
var ok = this.oldOnSubmi t();
if (ok)
return MyClass.onsubmi t(listId);
else
return false;
};
}
else
form.onsubmit = function(){MyCl ass.onsubmit(li stId);};
}

My problem is that the value returned from oldOnSubmit and stored in ok
appears as 'void'. This happens in IE 6 and in FireFox 1.07. Can anyone
explain what's happening?

TIA
Brian

Jan 9 '06 #1
8 2077
On 09/01/2006 08:12, bd****@fish.co. uk wrote:

[snip]
I have a form with an onsubmit event handler:
<form id="uploadForm " method="post" action="..."
onSubmit="check Dates()">

The event handler does some minor validation, then returns true or
false:
The onsubmit attribute defines - internally - another function. It is
equivalent to:

document.forms. uploadForm.onsu bmit = function(event) {
checkDates();
};

As this function does not have a return statement, its result will
always be the Undefined value (undefined).

<form id="uploadForm " ... onsubmit="retur n checkDates();">

[snip]
form.onsubmit = function(){MyCl ass.onsubmit(li stId);};


A similar thing occurs here; the return value of that call must be
returned from the calling function:

form.onsubmit = function() {return MyClass.onsubmi t(listId);};

[snip]

Hope that helps,
Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Jan 9 '06 #2
Thanks for that, but I still have a couple of questions:

1. Is it not the case that the semantics of onsubmit are such that if
it returns false the form is not submitted? How does this work if the
return value is always discarded?

2. More importantly, is there any way to do what I want to do, namely
to override onsubmit without losing the return value of the original
method?

Regards
Brian

Jan 9 '06 #3
Michael Winter wrote:
On 09/01/2006 08:12, bd****@fish.co. uk wrote:
I have a form with an onsubmit event handler:
<form id="uploadForm " method="post" action="..."
onSubmit="check Dates()">

The event handler does some minor validation, then returns true or
false:
The onsubmit attribute defines - internally - another function. It is
equivalent to:

document.forms. uploadForm.onsu bmit = function(event) {
checkDates();
};

As this function does not have a return statement, its result will
always be the Undefined value (undefined).


True.
<form id="uploadForm " ... onsubmit="retur n checkDates();">


It is not documented that returning a false-value cancels the event, but it
is documented that a boolean value either cancels or not cancels the event,
depending on the event type. Returning `false' cancels the `submit' event,
`true' does not.
PointedEars
Jan 9 '06 #4
VK

bdo...@fish.co. uk wrote:
Thanks for that, but I still have a couple of questions:

1. Is it not the case that the semantics of onsubmit are such that if
it returns false the form is not submitted? How does this work if the
return value is always discarded?

2. More importantly, is there any way to do what I want to do, namely
to override onsubmit without losing the return value of the original
method?


<form method="POST" action="your_UR L">

<!-- your form flow -->

<script type="text/javascript">
var b = '<input type="button" value="Submit" ';
b+= 'onclick="valid ate(this.form)" >';
document.write( b);
</script>

<noscript>
<input type = "submit" value="Submit">
</noscript>
</form>

Then later either myForm.submit() or not - depending on form check.
If JavaScript is not enabled then just regular submission w/o
client-side check.

....and be happy ever after :-)

P.S. If document.write( ) seems to you to be not refined enough for XXI
century :-) you can achieve the same in much more complex but more
"academical " way: still keep the conventional submit button as default
but replace its node on page load with simple button.

Jan 9 '06 #5
On 09/01/2006 18:26, VK wrote:

[snip]
<form method="POST" action="your_UR L">

<!-- your form flow -->

<script type="text/javascript">
var b = '<input type="button" value="Submit" ';
b+= 'onclick="valid ate(this.form)" >';
document.write( b);
</script>

<noscript>
<input type = "submit" value="Submit">
</noscript>
</form>


Why on Earth would the OP want to do something as ridiculous as that?

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Jan 9 '06 #6
VK

Michael Winter wrote:
On 09/01/2006 18:26, VK wrote:

[snip]
<form method="POST" action="your_UR L">

<!-- your form flow -->

<script type="text/javascript">
var b = '<input type="button" value="Submit" ';
b+= 'onclick="valid ate(this.form)" >';
document.write( b);
</script>

<noscript>
<input type = "submit" value="Submit">
</noscript>
</form>
Why on Earth would the OP want to do something as ridiculous as that?


That was my answer to:

bd****@fish.co. uk wrote: Thanks for that, but I still have a couple of questions:

1. Is it not the case that the semantics of onsubmit are such that if
it returns false the form is not submitted? How does this work if the
return value is always discarded?

2. More importantly, is there any way to do what I want to do, namely
to override onsubmit without losing the return value of the original
method?


By having a simple button (instead of submit) you are free from any
hassles to *return* anything (right away or any later). At it is
perfectly degradable in case of JavaScript disabled. So I admit missing
the ridiculousness of this approach?

Jan 10 '06 #7
On 10/01/2006 13:48, VK wrote:

[snip]
By having a simple button (instead of submit) you are free from any
hassles to *return* anything (right away or any later). [...] So I admit missing
the ridiculousness of this approach?
It is ridiculous because there is no hassle. I can only imagine that the
OP didn't notice the difference between his

<form ... onsubmit="check Dates();">

and

<form ... onsubmit="retur n checkDates();">

Furthermore, it's ridiculous in its excessiveness. Compare

<form method="POST" action="your_UR L">

<!-- your form flow -->

<script type="text/javascript">
var b = '<input type="button" value="Submit" ';
b+= 'onclick="valid ate(this.form)" >';
document.write( b);
</script>

<noscript>
<input type = "submit" value="Submit">
</noscript>
</form>

to

<form action="..." method="post"
onsubmit="retur n validate(this); "

<!-- ... -->
</form>

and then try and tell us, with a straight face, that the former is the
simpler, hassle-free option.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Jan 10 '06 #8
Mike & Lee, thanks for all your help.

VK & Thomas, thanks to you too, but I'm not going to be drawn into
philosophical discussions on this (but feel free to continue).

I realised the answers to my second post in the middle of the night (as
you do), but today has been a net-free day, so I couldn't reply.

Thanks again
Brian

Jan 10 '06 #9

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

Similar topics

4
2025
by: Neo Chou | last post by:
Greetings! I have a question about constant. I have a page like: ---------------------------------------------------------------------- <% Const adInteger = 3 'copied from adovbs.inc Const ucApple = 0 'user defined constant Const ucOrange = 1 'user defined constant
5
2172
by: Giannis Papadopoulos | last post by:
I have the following code #include <stdio.h> void a(void) { printf("a called.\n"); } int b(void) { printf("b called.\n");
2
2126
by: Gary Kahrau | last post by:
I need some help passing data to and from a usercontrol. In the following Property DisplayValue, If I hard code a return value (Return "Test Data"), then the data gets returned. If I work with any kind of assigned value, the return value is blank! Any idea on what I am doing wrong?
3
1840
by: Grzegorz ¦lusarek | last post by:
Hi all. I have situation that when my page is loaded i create js object <html> ... <script> function Page() { this.page = 0; this.result = 0 this.resultCount =1; this.currentPage =1; }
0
9687
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10484
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9072
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7565
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.