Submitting forms with Javascript/Konqueror

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Greg T

    #1

    Submitting forms with Javascript/Konqueror

    Hi,

    I have a rather long form that I don't want people submitting unless
    they are absolutely sure they are ready.

    I figured the easiest way to prevent an accidental form submission by
    way of hitting of the enter key was to simply not have a submit button,
    instead going for a regular anchor which calls a javascript confirmation
    box and then submits the form.

    To do this, I have one function, and an anchor that calls it:

    function submitConfirm()
    {
    var submitConfirmed = confirm("Are you ready to submit this form?");
    if( submitConfirmed == true )
    {
    document.survey Form.submit();
    }
    ....
    }

    <a href="#" onclick="submit Confirm()"><img src="submitBtn. png"
    alt="Submit"></a>


    All works fine in Mozilla and IE, but Konqueror doesn't behave as
    expected. Instead of submitting the form, the anchor (in this case,
    'http://my.site/BrokenSurvey.ht ml#') is loaded instead.

    I tried it using getElementsByNa me, but with the same result.

    Am I doing something wrong, or is it time to file a bug report with the
    KDE folks?

    thanks,
    Greg
  • Richard Cornford

    #2
    Re: Submitting forms with Javascript/Konqueror

    Greg T wrote:[color=blue]
    > I have a rather long form that I don't want people
    > submitting unless they are absolutely sure they are ready.
    >
    > I figured the easiest way to prevent an accidental form
    > submission by way of hitting of the enter key was to simply
    > not have a submit button, instead going for a regular
    > anchor which calls a javascript confirmation box and then
    > submits the form.[/color]

    You figured wrong. There is one obvious, simple and universally
    supported (subject of JS availability) palace to intercept the
    submission of any form and that is the - onsubmit - handler of the FORM
    element.

    With its use you can have the submit button that you should have (thus
    avoiding any JS dependency in this aspect of the system, and
    inconsistencies in the handling of your approach).

    <form name="surveyFor m" action=" ... " method="post"
    onsubmit="retur n confirm('Are you ready to submit this form?');">
    [color=blue]
    > To do this, I have one function, and an anchor that calls it:
    >
    > function submitConfirm()
    > {
    > var submitConfirmed = confirm("Are you ready to submit this form?");
    > if( submitConfirmed == true )
    > {
    > document.survey Form.submit();
    > }
    > ...
    > }[/color]

    Would have been simpler as:-

    function submitConfirm() {
    if(confirm("Are you ready to submit this form?")){
    document.survey Form.submit();
    }
    }
    [color=blue]
    > <a href="#" onclick="submit Confirm()"><img
    > src="submitBtn. png" alt="Submit"></a>[/color]

    When an - onclick - handler does not return boolean false it is not
    cancelling the navigation specified in the HREF. It is normal for
    browsers to assume that the last interaction with a page in terms of
    form submitting or navigation represents the user's current desire. So
    navigating to the specified HREF should be the expected result of this
    operation because the navigation follows the - onclick - function's
    execution.

    You could change the onclick attribute to "submitConfirm( );return
    false;" and cancel the navigation, but you would be better off using the
    FORM element's onsubmit handler.
    [color=blue]
    > All works fine in Mozilla and IE,[/color]

    Consider yourself luck that it does because that is not the behaviour
    that you code is strictly asking for.
    [color=blue]
    > but Konqueror doesn't behave as
    > expected.
    > Instead of submitting the form, the anchor (in this case,
    > 'http://my.site/BrokenSurvey.ht ml#') is loaded instead.[/color]

    But it is doing what you are asking for in this case. i.e. it submits
    the form (an asynchronous process that takes some time) and then it is
    navigating to follow the HREF, cancelling the pending submission as it
    is no longer interested in displaying the returned response.
    [color=blue]
    > I tried it using getElementsByNa me, but with the same result.[/color]

    I can see no relevance in this.
    [color=blue]
    > Am I doing something wrong, or is it time to file a bug
    > report with the KDE folks?[/color]

    There is no point in bothering the KDE folks with this, including a
    submit button and using the - onsubmit - handler will produce
    cross-browser results and consistent behaviour.

    Richard.


    Comment

    • Greg T

      #3
      Re: Submitting forms with Javascript/Konqueror

      Richard Cornford wrote:[color=blue]
      > Greg T wrote:
      >[color=green]
      >>I have a rather long form that I don't want people
      >>submitting unless they are absolutely sure they are ready.
      >>
      >>I figured the easiest way to prevent an accidental form
      >>submission by way of hitting of the enter key was to simply
      >>not have a submit button, instead going for a regular
      >>anchor which calls a javascript confirmation box and then
      >>submits the form.[/color]
      >
      >
      > You figured wrong. There is one obvious, simple and universally
      > supported (subject of JS availability) palace to intercept the
      > submission of any form and that is the - onsubmit - handler of the FORM
      > element.
      >
      > With its use you can have the submit button that you should have (thus
      > avoiding any JS dependency in this aspect of the system, and
      > inconsistencies in the handling of your approach).
      >
      > <form name="surveyFor m" action=" ... " method="post"
      > onsubmit="retur n confirm('Are you ready to submit this form?');">
      >[color=green]
      >><a href="#" onclick="submit Confirm()"><img
      >>src="submitBt n.png" alt="Submit"></a>[/color]
      >
      >
      > When an - onclick - handler does not return boolean false it is not
      > cancelling the navigation specified in the HREF. It is normal for
      > browsers to assume that the last interaction with a page in terms of
      > form submitting or navigation represents the user's current desire. So
      > navigating to the specified HREF should be the expected result of this
      > operation because the navigation follows the - onclick - function's
      > execution.
      >
      > You could change the onclick attribute to "submitConfirm( );return
      > false;" and cancel the navigation, but you would be better off using the
      > FORM element's onsubmit handler.
      >[/color]
      ....

      Thanks for your help, Richard. I was hoping it wouldn't come to the
      onsubmit handler. I was looking for a way that would fail if javascript
      was disabled, rather than submit the form without prompting.

      Looks like your way is good, if I chuck in some detection code.

      thanks again,

      Greg

      Comment

      Working...