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

B

bdobby

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) {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
 
M

Michael Winter

On 09/01/2006 08:12, (e-mail address removed) 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
 
B

bdobby

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
 
T

Thomas 'PointedEars' Lahn

Michael said:
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
 
V

VK

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.
 
L

Lee

(e-mail address removed) said:
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?

You're confused about what the onsubmit handler is.
In your case it is NOT the function checkDates().
That's a function which is *called by* the onsubmit handler.

The string value of the onsubmit HTML attribute is parsed by the
script engine and becomes the body of the onsubmit() method.

Your onsubmit handler doesn't return any value because you
haven't included a return statement:

onsubmit="return checkDates()"
 
M

Michael Winter

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
 
V

VK

Michael said:
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:

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?
 
L

Lee

VK said:
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?

A standard submit button mediated by a value returned from the onsubmit
handler is simpler and also perfectly degradable in case Javascript is
disabled.
 
M

Michael Winter

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
 
B

bdobby

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top