onSubmit ALWAYS returns true when it sets location.href?

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

    #1

    onSubmit ALWAYS returns true when it sets location.href?

    Hello,

    I am trying to call Javascript from FORM's onSubmit, and return false,
    so the form is not actually submitted.
    Normally I can just add 'return false;' to onSubmit to accomplish
    this, like so:

    <form name="XX" method="get" action="/Foo" onsubmit="onSea rch();
    return false;">

    However, if my Javascript sets 'location.href' , then my 'return
    false;' is never executed:

    <script type="text/javascript" language="JavaS cript">
    <!--
    function onSearch() {
    location.href = "/Bar?q=" +
    encode(document .forms["XX"].elements["q"].value);
    }
    // -->
    </script>


    Is this a known behaviour? Are there any workarounds?

    What is happening here is that instead of location.href getting set to
    /Bar?q=..... and the browser going to that URL, my form's /Foo action
    gets called, and that is what I am trying to avoid. Any ideas how I
    can avoid /Foo getting called?

    Thanks!
  • Randy Webb

    #2
    Re: onSubmit ALWAYS returns true when it sets location.href?

    OtisUsenet wrote:
    [color=blue]
    > Hello,
    >
    > I am trying to call Javascript from FORM's onSubmit, and return false,
    > so the form is not actually submitted.[/color]

    Then why not just use a button?

    <input type="button" value="Somethin g" onclick="onSear ch()">

    <form onsubmit="retur n false">

    Now, the form won't get submitted at all if scripting is enabled.

    <--snip-->

    --
    Randy
    comp.lang.javas cript FAQ - http://jibbering.com/faq

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: onSubmit ALWAYS returns true when it sets location.href?

      OtisUsenet wrote:
      [color=blue]
      > I am trying to call Javascript from FORM's onSubmit, and return false,
      > so the form is not actually submitted.
      > Normally I can just add 'return false;' to onSubmit to accomplish
      > this, [...]
      > However, if my Javascript sets 'location.href' , then my 'return
      > false;' is never executed:
      >
      > <script type="text/javascript" language="JavaS cript">[/color]

      language="JavaS cript" is not needed anymore. With
      (X)HTML Strict and XHTML 1.1 it is disallowed.
      [color=blue]
      > <!--[/color]

      Commenting out scripts is an obsoleted technique since there
      are no working UAs out there that require it. HTML 2.0 has
      been obsoleted and HTML 3.2 defines the "script" element.
      [color=blue]
      > function onSearch() {
      > location.href = "/Bar?q=" +
      > encode(document .forms["XX"].elements["q"].value);
      > }[/color]

      What is this encode() method? It is not a built-in.
      Are you sure you are not getting script errors? If no,
      that would explain why "return false" is not executed.

      If document.forms["XX"] is the form you try to submit,
      pass a `this' reference to the method in its onsubmit
      handler and use the method's argument instead.
      [color=blue]
      > // -->[/color]

      Remove that, too.
      [color=blue]
      > </script>
      >
      >
      > Is this a known behaviour?[/color]

      Yes, changing the current container element's resource should result
      in all information of the contents (document) to be lost.
      [color=blue]
      > Are there any workarounds?[/color]

      Don't do it.
      [color=blue]
      > What is happening here is that instead of location.href getting set to
      > /Bar?q=..... and the browser going to that URL, my form's /Foo action
      > gets called, and that is what I am trying to avoid.[/color]

      Unless there are script errors, I would consider *that* a bug. Once the
      location changes, the other resource should be accessed and no code of the
      previous resource should be executed.
      [color=blue]
      > Any ideas how I can avoid /Foo getting called?[/color]

      If there are no script errors, use either working UA or no onsubmit handler.
      I wonder why you even change `location' on submit.


      PointedEars
      --
      "You have to mount it before you fsck it."

      Comment

      Working...