473,386 Members | 1,708 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,386 software developers and data experts.

'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="checkDates()">
The event handler does some minor validation, then returns true or
false:
function checkDates(y, m, d) {
snip<<

if (endDate.getTime() >= startDate.getTime())
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.setOnSubmit = function(listId) {
var list = document.getElementById(listId);
var form = list.form;

var f = form.onsubmit;
if (typeof f == "function") {
form.oldOnSubmit = f;
form.onsubmit = function(){
var ok = this.oldOnSubmit();
if (ok)
return MyClass.onsubmit(listId);
else
return false;
};
}
else
form.onsubmit = function(){MyClass.onsubmit(listId);};
}

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 2033
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="checkDates()">

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.onsubmit = 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="return checkDates();">

[snip]
form.onsubmit = function(){MyClass.onsubmit(listId);};


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

form.onsubmit = function() {return MyClass.onsubmit(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="checkDates()">

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.onsubmit = 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="return 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_URL">

<!-- your form flow -->

<script type="text/javascript">
var b = '<input type="button" value="Submit" ';
b+= 'onclick="validate(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_URL">

<!-- your form flow -->

<script type="text/javascript">
var b = '<input type="button" value="Submit" ';
b+= 'onclick="validate(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_URL">

<!-- your form flow -->

<script type="text/javascript">
var b = '<input type="button" value="Submit" ';
b+= 'onclick="validate(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="checkDates();">

and

<form ... onsubmit="return checkDates();">

Furthermore, it's ridiculous in its excessiveness. Compare

<form method="POST" action="your_URL">

<!-- your form flow -->

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

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

to

<form action="..." method="post"
onsubmit="return 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
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...
5
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
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...
3
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.