href onclick

C

Christoph

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
 
J

josh

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
 
L

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.


--
 
T

Thomas 'PointedEars' Lahn

Christoph said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top