href onclick

Discussion in 'Javascript' started by Christoph, Sep 11, 2007.

  1. Christoph

    Christoph Guest

    I have set up the onclick event of an href so that it submits a form
    and returns false. I'm returning false to prevent the browser from
    actually going to the page. However, because I have the form
    submission as part of the onclick event, it seems like I'm not being
    successful in preventing that.

    Is there some way I can make it so that the browser doesn't go to the
    page?

    thnx,
    Chris
    Christoph, Sep 11, 2007
    #1
    1. Advertising

  2. Christoph

    josh Guest

    On 11 Set, 14:20, Christoph <> wrote:
    > I have set up the onclick event of an href so that it submits a form
    > and returns false. I'm returning false to prevent the browser from
    > actually going to the page. However, because I have the form
    > submission as part of the onclick event, it seems like I'm not being
    > successful in preventing that.
    >
    > Is there some way I can make it so that the browser doesn't go to the
    > page?
    >
    > thnx,
    > Chris


    this code should do it:

    <a href="javascript: void foo()">text</a>

    or

    // here foo() MUST return false that is then returned to onclick
    <a href="#" onclick="return foo()">text</a>

    Bye
    josh, Sep 11, 2007
    #2
    1. Advertising

  3. Christoph

    Lee Guest

    Christoph said:
    >
    >I have set up the onclick event of an href so that it submits a form
    >and returns false. I'm returning false to prevent the browser from
    >actually going to the page. However, because I have the form
    >submission as part of the onclick event, it seems like I'm not being
    >successful in preventing that.
    >
    >Is there some way I can make it so that the browser doesn't go to the
    >page?


    Are you trying to see how far you can distort the normal functionality?
    Using a link as something other than a link, and then trying to use
    form submission for something other than what it was designed to do?

    Submitting a form loads the new page sent back from the server.
    If you don't want to load a new page, you don't want to submit a form.
    Find another method of sending your data to the server.


    --
    Lee, Sep 11, 2007
    #3
  4. Christoph wrote:
    > I have set up the onclick event of an href so that it submits a form
    > and returns false.


    Wrong approach.

    > I'm returning false to prevent the browser from actually going to the page.


    Why should it? You are canceling the click event of the link, thereby
    preventing its default action, which is to navigate to the resource its
    `href' attribute designates.

    > However, because I have the form submission as part of the onclick event,
    > it seems like I'm not being successful in preventing that.


    Yes, your approach is wrong. When the form is submitted through your
    event listener, navigation to another resource has already taken place.

    > Is there some way I can make it so that the browser doesn't go to the
    > page?


    Use the correct approach: a submit button (input[type="submit"] or
    input[type="image"]) and the `onsubmit' event handler of the `form'
    element to which you return `false' when the form should not be
    submitted. You will have to test server-side anyway.


    PointedEars
    --
    Prototype.js was written by people who don't know javascript for people
    who don't know javascript. People who don't know javascript are not
    the best source of advice on designing systems that use javascript.
    -- Richard Cornford, cljs, <f806at$ail$1$>
    Thomas 'PointedEars' Lahn, Sep 11, 2007
    #4
    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. CRON
    Replies:
    24
    Views:
    200,427
    Adrienne Boswell
    Jun 20, 2006
  2. Soren Vejrum
    Replies:
    4
    Views:
    517
    Lasse Reichstein Nielsen
    Jul 5, 2003
  3. saiho.yuen
    Replies:
    3
    Views:
    402
    kaeli
    Sep 14, 2004
  4. Replies:
    2
    Views:
    440
  5. Vincent van Beveren

    BASE HREF and A HREF="#" onclick="..."

    Vincent van Beveren, Jul 6, 2006, in forum: Javascript
    Replies:
    2
    Views:
    290
    Vincent van Beveren
    Jul 6, 2006
Loading...

Share This Page