Solved

Cancel href via an onclick - or detect javascript based form submission

Posted on 2006-11-16
7
1,831 Views
Last Modified: 2012-06-27
I have a link generated by asp.net which submits my form

<a href="javascript:generatedfunction();">Click Me</a>

I wanted to be able to trigger an event on submit of the form.  However since the form is submitted via javascript I could not just add to the onSubmit of the form (did not seem to work), but I was able to insert my own function that triggers first in the onclick:

<a onclick="myfunction();" href="javascript:generatedfunction();">

This works.

But now what I want to do is to prevent the form submission based on a certain form element's value.  I know with a standard form submit you can simply return false in the onSubmit, but this does not work when the form is submitted via javascript as is the case with the generated function.

So my question is how/what code can I add to be able to prevent the form submission (i.e. the generated function) from firing in the specific situation?

Or is there another way to intercept the form submit event - even if it is triggered via javascript (i.e. document.formname.submit()) - and do the equivalent of a return false to cancel the submit
0
Comment
Question by:mrichmon
  • 4
  • 3
7 Comments
 
LVL 33

Accepted Solution

by:
knightEknight earned 500 total points
ID: 17961586
return true from the onclick to submit the form, return false otherwise:

<a onclick="return myfunction();" href="javascript:generatedfunction();">


function myfuncion()
{
   if ( some condition )
       return true;
   else
      return false;
}
0
 
LVL 35

Author Comment

by:mrichmon
ID: 17961775
I thought I tried that.

I'll try again...
0
 
LVL 35

Author Comment

by:mrichmon
ID: 17961782
Okay I did try again - I put just "return false;" - no if clauses or anything - to guarantee it was returning false.

So my entire function looked like:

function myfunction()
{
   alert("here");
   return false;
}

The alert guarantees me the function was firing.  But it still fired the generated function next.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 33

Expert Comment

by:knightEknight
ID: 17961783
of course, I mis-spelled the function name.  (gotta watch that!)
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 17961787
did you change the onclick to include the return clause?

onclick="return myfunction();"
0
 
LVL 35

Author Comment

by:mrichmon
ID: 17961790
Oh I missed the return in the onclick both times.

That did the trick.

Thanks!
0
 
LVL 35

Author Comment

by:mrichmon
ID: 17961822
Strangely enough if there is something that it doesn't like in the function it ignores the return false.

For example:

alert();

with no content.

oh well, good enough.

Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now