Multiple onsubmit calls

Discussion in 'Javascript' started by DrKen, Jun 22, 2011.

  1. DrKen

    DrKen Guest

    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
    DrKen, Jun 22, 2011
    #1
    1. Advertising

  2. 2011-06-22 19:47, DrKen wrote:

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

    --
    Yucca, http://www.cs.tut.fi/~jkorpela/
    Jukka K. Korpela, Jun 22, 2011
    #2
    1. Advertising

  3. DrKen

    DrKen Guest

    On Jun 22, 10:33 am, "Jukka K. Korpela" <> wrote:
    > 2011-06-22 19:47, DrKen wrote:
    > >    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.
    >
    > --
    > Yucca,http://www.cs.tut.fi/~jkorpela/


    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
    DrKen, Jun 22, 2011
    #3
  4. Jukka K. Korpela wrote:

    > 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=%2Ftrunk%2Fobject.js&rev=218&peg=218>
    --
    Use any version of Microsoft Frontpage to create your site.
    (This won't prevent people from viewing your source, but no one
    will want to steal it.)
    -- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
    Thomas 'PointedEars' Lahn, Jun 22, 2011
    #4
  5. 2011-06-22 20:51, DrKen wrote:

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

    --
    Yucca, http://www.cs.tut.fi/~jkorpela/
    Jukka K. Korpela, Jun 22, 2011
    #5
  6. On Wed, 22 Jun 2011 09:47:31 -0700, DrKen wrote:

    > 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
    Denis McMahon, Jun 22, 2011
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Honne Gowda A
    Replies:
    2
    Views:
    861
    Karl Heinz Buchegger
    Oct 31, 2003
  2. andy6
    Replies:
    2
    Views:
    752
    andy6 via DotNetMonster.com
    Jun 9, 2006
  3. Richard Tobin
    Replies:
    24
    Views:
    772
  4. CharitiesOnline.co.uk

    multiple calls to scripts with onSubmit

    CharitiesOnline.co.uk, Sep 26, 2003, in forum: Javascript
    Replies:
    0
    Views:
    76
    CharitiesOnline.co.uk
    Sep 26, 2003
  5. onsubmit - multiple actions

    , Jul 31, 2006, in forum: Javascript
    Replies:
    2
    Views:
    116
Loading...

Share This Page