By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,557 Members | 1,406 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,557 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.