Multiple onsubmit calls

D

DrKen

I am working on making changes to an existing page (as evident from
previous posts) that has multiple methods that should be executed
"onsubmit". I dn't want to break those. So if there are, say, five
JavaScript functions in one or more <script> </script> blocks, are
these executed in the order that the onsubmit invocations occur on the
page? If one of these fails, does that make the others fail? I am
guessing that since the function should return true or false that if
it returns false, nothing else is executed. Thanks.

Ken
 
J

Jukka K. Korpela

I am working on making changes to an existing page

It would then be a good idea to consider posting the URL.
(as evident from previous posts)

I think there are about a million previous posts. Don't expect us to
trace back what you might have posted "recently".
that has multiple methods that should be executed
"onsubmit".

That does not quite parse. Well, it parses, but the meaning remains
obscure. I guess you mean that the page has a form with an onsubmit
attribute, the value of which contains several function calls, like
onsubmit="foo(); bar(); zap(1,2,3)". Whether the functions are methods
is immaterial here.
I dn't want to break those.

A good idea. Keep your code clean, don't mess around with global
variables unless you really have to.
So if there are, say, five
JavaScript functions in one or more<script> </script> blocks, are
these executed in the order that the onsubmit invocations occur on the
page?

Functions are not executed at all, unless they are called. When called,
they are executed in the order of the calls (invocations). The onsubmit
attributes as such do not cause any function calls. They are event
handlers, so the order depends on what the user does. But when a form is
being submitted, then its onsubmit attribute is executed. If it is
onsubmit="foo(); bar(); zap(1,2,3)", then the functions are called in
that order.
If one of these fails, does that make the others fail?

It depends on the meaning of "fails". Just doing something else than it
was meant to be does not affect others. But if an exception is thrown
and the exception is not handled, then the rest of script execution is
skipped. So if you prepend a function call of your own, say
onsubmit="zip(); foo(); bar(); zap(1,2,3)"
then the other functions won't be called if your function, say, tries to
use a.b when the value of a is undefined.

A fairly brutal but effective way to avoid such problems is to use the
try statement so that exceptions thrown in your function will just be
discarded:
onsubmit="try { zip() } catch(e) {}; foo(); bar(); zap();"
In reality, you would of course use the try statement inside your
function and make it at least report the errors:

function zip() {
try {
// do whatever zips do here
} catch(e) {
alert('Error (code ' + e.name + '): ' + e.message); }}
I am
guessing that since the function should return true or false that if
it returns false, nothing else is executed.

No, the function return value is just ignored (discarded), unless the
program code does something with it.
 
D

DrKen

It would then be a good idea to consider posting the URL.

 > (as evident from previous posts)

I think there are about a million previous posts. Don't expect us to
trace back what you might have posted "recently".


That does not quite parse. Well, it parses, but the meaning remains
obscure. I guess you mean that the page has a form with an onsubmit
attribute, the value of which contains several function calls, like
onsubmit="foo(); bar(); zap(1,2,3)". Whether the functions are methods
is immaterial here.

 > I dn't want to break those.

A good idea. Keep your code clean, don't mess around with global
variables unless you really have to.

 > So if there are, say, five


Functions are not executed at all, unless they are called. When called,
they are executed in the order of the calls (invocations). The onsubmit
attributes as such do not cause any function calls. They are event
handlers, so the order depends on what the user does. But when a form is
being submitted, then its onsubmit attribute is executed. If it is
onsubmit="foo(); bar(); zap(1,2,3)", then the functions are called in
that order.


It depends on the meaning of "fails". Just doing something else than it
was meant to be does not affect others. But if an exception is thrown
and the exception is not handled, then the rest of script execution is
skipped. So if you prepend a function call of your own, say
onsubmit="zip(); foo(); bar(); zap(1,2,3)"
then the other functions won't be called if your function, say, tries to
use a.b when the value of a is undefined.

A fairly brutal but effective way to avoid such problems is to use the
try statement so that exceptions thrown in your function will just be
discarded:
onsubmit="try { zip() } catch(e) {}; foo(); bar(); zap();"
In reality, you would of course use the try statement inside your
function and make it at least report the errors:

function zip() {
   try {
     // do whatever zips do here
   } catch(e) {
     alert('Error (code ' + e.name + '): ' + e.message); }}

 > I am


No, the function return value is just ignored (discarded), unless the
program code does something with it.

Thanks for the information. I have two follow up questions.
1. I see this bit of code in a JavaScript block:
return validEmailFields;
}//Connect submit event to checkFieldsOnSubmit method
document.AnnForm.onsubmit = checkFieldsOnSubmit;


There is code for the complete function above the return line, so the
return statement and the following curly brace end the function, so
far as I can tell. After the last statement above, there is another
function declaration. So how can the third statement,
docuemtn.AnnForm.onsubmit... occur outside of any function? How does
that work? I thought that everything except var declarations had to
be in a function.

2. I "take back" the initial question. I see several functions with
"OnSubmit" as part of the function name, but there are no references
in the form itself to any action when the Submit button is clicked.
So is the statement
document.AnnForm.onsubmit = checkFieldsOnSubmit;

somehow equivalent to putting an onsubmit() call on some HTML
element? Thanks.

Ken
 
T

Thomas 'PointedEars' Lahn

Jukka said:
In reality, you would of course use the try statement inside your
function and make it at least report the errors:

function zip() {
try {
// do whatever zips do here
} catch(e) {
alert('Error (code ' + e.name + '): ' + e.message); }}

If at all possible, non-i18n'd error reports for the developer – which
exceptions should be – should go to the error console, not to the user
interface;

console.log('Error (code ' + e.name + '): ' + e.message);

Incidentally, many error consoles support that by default, either by calling
e.toString() or other use of e's properties:

console.log(e);

jsx.dmsg(), jsx.info(), and jsx.warn(), from JSX:eek:bject.js, provide
appropriate wrappers. [1] (Just found a little bug in jsx.dmsg() in
trunk/object.js@218: If opera.postError() is called, the method should
return `true', too. Will be fixed in the next revision.)


PointedEars
___________
[1]
<http://pointedears.de/websvn/filedetails.php?repname=JSX&path=/trunk/object.js&rev=218&peg=218>
 
J

Jukka K. Korpela

1. I see this bit of code in a JavaScript block:
return validEmailFields;
}//Connect submit event to checkFieldsOnSubmit method
document.AnnForm.onsubmit = checkFieldsOnSubmit;

There is code for the complete function above the return line, so the
return statement and the following curly brace end the function, so
far as I can tell.

This could be confirmed if we could see the URL, but it sounds plausible
After the last statement above, there is another
function declaration. So how can the third statement,
docuemtn.AnnForm.onsubmit... occur outside of any function?

It is just global code outside functions. It can be viewed as part of
the main program. It normally gets executed when the browser has reached
that part in reading and parsing code.
I thought that everything except var declarations had to
be in a function.

No, and if it had, how would anything get executed ever? Well, in
client-side JavaScript, you can use event handler so that some event,
such as completion of loading the document, effectively starts the
program execution. But at least in simple use of JavaScript, execution
starts so that global code starts doing something and calling functions.
So is the statement
document.AnnForm.onsubmit = checkFieldsOnSubmit;

somehow equivalent to putting an onsubmit() call on some HTML
element?

It is rougly speaking equivalent to putting the attribute
onsubmit="checkFieldsOnSubmit()" to the <form> element that has
name="AnnForm".
 
D

Denis McMahon

if there are [several]
JavaScript functions in one or more <script> </script> blocks, are these
executed in the order that the onsubmit invocations occur on the page?
If one of these fails, does that make the others fail? I am guessing
that since the function should return true or false that if it returns
false, nothing else is executed.

functions are executed when they are called by event handlers, other
functions, etc, in the order in which they are called.

If I have:

<script type="text/javascript">
function f1() {document.getElementById("theId").value = 1;}
function f2() {document.getElementById("theId").value = 2;}
function f3() {document.getElementById("theId").value = 3;}
</script>

and an element in my html:

<input type="button" value="Click Here" name="theName" id="theId"
onclick="f3();">

Then, when the button is clicked, only function f3 is executed.

I suspect, however, that your question may be missing some information
that is relevant to answering it correctly.

Perhaps you could offer a url or simplified code example to explain the
scenario you are describing?

Rgds

Denis McMahon
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top