exceedingly annoying javascript behavior

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

    exceedingly annoying javascript behavior

    just another thing.. for people who are trying to replace a url outside
    of a form and who are searching for an answer - when you are trying to
    use the document.locati on.replace("") method for going to another url,
    you have to put false on the end of your return statement and call the
    function in the form of:

    onsubmit="retur n(make_url());"

    Why? Apparently there is an 'event bubble' that happens when you are
    doing a POST method call, which proceeds to overwrite your hand-crafted
    url with the contents of the form, which causes it to revert back to
    the old, pre-document.locati on.replace("") value (and cause you to lose
    much hair from pulling in the process).

    Anyways,

    return false;

    at the end of make_url() seems to short-ciruit this from happening.

    What an annoying behaviour - that's 1 1/2 hours down the drain right
    there. Who designed this language anyway?

    Ed

  • Lee

    #2
    Re: exceedingly annoying javascript behavior

    horos said:[color=blue]
    >
    >just another thing.. for people who are trying to replace a url outside
    >of a form and who are searching for an answer - when you are trying to
    >use the document.locati on.replace("") method for going to another url,
    >you have to put false on the end of your return statement and call the
    >function in the form of:
    >
    >onsubmit="retu rn(make_url()); "
    >
    >Why? Apparently there is an 'event bubble' that happens when you are
    >doing a POST method call, which proceeds to overwrite your hand-crafted
    >url with the contents of the form, which causes it to revert back to
    >the old, pre-document.locati on.replace("") value (and cause you to lose
    >much hair from pulling in the process).
    >
    >Anyways,
    >
    > return false;
    >
    >at the end of make_url() seems to short-ciruit this from happening.
    >
    >What an annoying behaviour - that's 1 1/2 hours down the drain right
    >there. Who designed this language anyway?[/color]

    It's actually very reasonable for the submit() method to submit
    the form. If you want to do something other than submit the
    form, you should replace your submit button with a standard
    button.

    Comment

    • Fred Oz

      #3
      Re: exceedingly annoying javascript behavior

      horos wrote:[color=blue]
      > just another thing.. for people who are trying to replace a url outside
      > of a form and who are searching for an answer - when you are trying to
      > use the document.locati on.replace("") method for going to another url,
      > you have to put false on the end of your return statement and call the
      > function in the form of:
      >
      > onsubmit="retur n(make_url());"[/color]

      Or:

      onsubmit="make_ url(); return false;"
      [color=blue]
      >
      > Why? Apparently there is an 'event bubble' that happens when you are
      > doing a POST method call, which proceeds to overwrite your hand-crafted
      > url with the contents of the form, which causes it to revert back to
      > the old, pre-document.locati on.replace("") value (and cause you to lose
      > much hair from pulling in the process).[/color]

      No, it goes to a new URL based on the results of submitting the
      form which happens *after* the onsubmit finishes.

      Bone up on the DOM event model:

      <URL:http://www.w3.org/TR/DOM-Level-2-Events/events.html#Eve nts-flow>

      And have look here for some excellent advice and information:

      <URL:http://www.quirksmode. org/js/>

      It's presented in frames (but supports frameless if you prefer),
      follow the "W3C DOM" link, then the "forms" link.

      There is also some good stuff under "JavaScript " --> "Events".

      <URL:http://www.quirksmode. org/js/introevents.htm l>

      [color=blue]
      >
      > Anyways,
      >
      > return false;
      >
      > at the end of make_url() seems to short-ciruit this from happening.[/color]

      Because the onsubmit will return false, stopping the form from
      submitting.
      [color=blue]
      >
      > What an annoying behaviour - that's 1 1/2 hours down the drain right
      > there. Who designed this language anyway?[/color]

      What you frustrated about is not JavaScript per se, but how it works
      when combined with HTML and the DOM. When you click a submit button,
      the form will submit unless the onsubmit tells it not to (returns
      false).

      The form is not submitted by JavaScript, it is 'submitted' by the
      browser based on the HTML - JavaScript (or any other scripting
      language supported by your browser) just provides functionality to
      modify things. Turn off JavaScript (or script support in general)
      and the form will submit no matter what you have scripted. That is
      not unreasonable, it's what the HTML specification says should
      happen.

      It seems you are using a form and form elements when you don't want
      the basic functionality of a form. You do not have to put form
      elements inside a form - you can just put them anywhere in the page,
      then use their name or id to reference them. If not in a form, best
      to use an id and reference them using getElementById or you could use
      getElementsByTa gName to get say all the input elements.

      Why not post an example or description of what you are trying to do
      and let the experts here tell you how best to do it? You may be
      surprised at how simple it is.




      --
      Fred

      Comment

      Working...