Question about which is the correct style of JavaScript to pass to FORM's onSubmit attribute

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sean Dockery

    Question about which is the correct style of JavaScript to pass to FORM's onSubmit attribute

    Which is the following is correct?

    a) <form ... onSubmit="retur n checkData()">

    b) <form ... onSubmit="retur n checkData();">

    c) <form ... onSubmit="check Data()">

    d) <form ... onSubmit="check Data();">

    ....where checkData is a JavaScript function that returns a true or false
    value.

    It seems that Internet Explorer accepts all of the above, but I'm not
    taking that to mean that all are correct.

    I've attempted to search the W3 web site on the terms form and onsubmit,
    but the documentation refers to the attribute value as being an intrinsic
    event (without giving any examples). The archived mailing lists contain
    messages which demonstrate all of the options that I've provided above. I
    gave up after the forth page of search results because of the number of
    conflicting examples.

    Does anyone know which is correct?

    Thanks for your precious time.



  • Michael Winter

    #2
    Re: Question about which is the correct style of JavaScript to pass to FORM's onSubmit attribute

    On Mon, 15 Nov 2004 15:51:19 GMT, Sean Dockery <sdockery@secur ac.net>
    wrote:
    [color=blue]
    > Which is the following is correct?
    >
    > a) <form ... onSubmit="retur n checkData()">
    >
    > b) <form ... onSubmit="retur n checkData();">
    >
    > c) <form ... onSubmit="check Data()">
    >
    > d) <form ... onSubmit="check Data();">
    >
    > ...where checkData is a JavaScript function that returns a true or false
    > value.[/color]

    All of them are correct in that they will result in the call of checkData.
    However, only a) and b) will actually affect whether the form is submitted.

    The content of an intrinsic event attribute becomes the body of an
    anonymous function:

    /* Note that IE doesn't pass an event argument. It uses a global
    * variable of the same name. Either way, you can still use:
    *
    * on<event>="myFu nction(event)"
    *
    * to access the event object.
    */
    formElement.ons ubmit = function(event) {
    /* Value in onsubmit attribute */
    };

    Obviously, if you don't include a return statement, no value will actually
    be returned when this anonymous function exits.

    With regards to the semicolon, the same rules apply as normal: semicolons
    are generally optional, but it's good practice to include them. If you had
    two or more statements/expressions to evaluate, a semicolon would be
    necessary to separate them.

    The final point I like to make is that it's simpler to pass a reference to
    the FORM when it is called, rather than obtain one later. That is:

    <form ... onsubmit="retur n checkData(this) ;">

    function checkData(form) {
    /* You can now use form, rather than
    * document.forms['formName']
    */
    }
    [color=blue]
    > It seems that Internet Explorer accepts all of the above, but I'm not
    > taking that to mean that all are correct.[/color]

    As I said, they are all syntactically correct, but the second two are
    probably not what you want.

    [snip]

    Hope that helps,
    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • Sean Dockery

      #3
      Re: Question about which is the correct style of JavaScript to pass to FORM's onSubmit attribute

      Thanks, Michael. Your response was extremely helpful, and exactly what I
      was looking for.

      "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
      news:opshiklmsx x13kvk@atlantis ...[color=blue]
      > On Mon, 15 Nov 2004 15:51:19 GMT, Sean Dockery <sdockery@secur ac.net>
      > wrote:
      >[color=green]
      >> Which is the following is correct?
      >>
      >> a) <form ... onSubmit="retur n checkData()">
      >>
      >> b) <form ... onSubmit="retur n checkData();">
      >>
      >> c) <form ... onSubmit="check Data()">
      >>
      >> d) <form ... onSubmit="check Data();">
      >>
      >> ...where checkData is a JavaScript function that returns a true or false
      >> value.[/color]
      >
      > All of them are correct in that they will result in the call of
      > checkData. However, only a) and b) will actually affect whether the form
      > is submitted.
      >
      > The content of an intrinsic event attribute becomes the body of an
      > anonymous function:
      >
      > /* Note that IE doesn't pass an event argument. It uses a global
      > * variable of the same name. Either way, you can still use:
      > *
      > * on<event>="myFu nction(event)"
      > *
      > * to access the event object.
      > */
      > formElement.ons ubmit = function(event) {
      > /* Value in onsubmit attribute */
      > };
      >
      > Obviously, if you don't include a return statement, no value will
      > actually be returned when this anonymous function exits.
      >
      > With regards to the semicolon, the same rules apply as normal: semicolons
      > are generally optional, but it's good practice to include them. If you
      > had two or more statements/expressions to evaluate, a semicolon would be
      > necessary to separate them.
      >
      > The final point I like to make is that it's simpler to pass a reference
      > to the FORM when it is called, rather than obtain one later. That is:
      >
      > <form ... onsubmit="retur n checkData(this) ;">
      >
      > function checkData(form) {
      > /* You can now use form, rather than
      > * document.forms['formName']
      > */
      > }
      >[color=green]
      >> It seems that Internet Explorer accepts all of the above, but I'm not
      >> taking that to mean that all are correct.[/color]
      >
      > As I said, they are all syntactically correct, but the second two are
      > probably not what you want.
      >
      > [snip]
      >
      > Hope that helps,
      > Mike
      >
      > --
      > Michael Winter
      > Replace ".invalid" with ".uk" to reply by e-mail.[/color]


      Comment

      Working...