469,309 Members | 2,789 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,309 developers. It's quick & easy.

href onclick

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

Sep 11 '07 #1
3 2553
On 11 Set, 14:20, Christoph <christoph.bo...@gmail.comwrote:
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

Sep 11 '07 #2
Lee
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.
--

Sep 11 '07 #3
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, <f8*******************@news.demon.co.uk>
Sep 11 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Jez | last post: by
11 posts views Thread by Dana Smith | last post: by
14 posts views Thread by Brandon Hoppe | last post: by
4 posts views Thread by Jamie Jackson | last post: by
53 posts views Thread by usenet | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.