Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1846
  • Last Modified:

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

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
mrichmon
Asked:
mrichmon
  • 4
  • 3
1 Solution
 
knightEknightCommented:
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
 
mrichmonAuthor Commented:
I thought I tried that.

I'll try again...
0
 
mrichmonAuthor Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
knightEknightCommented:
of course, I mis-spelled the function name.  (gotta watch that!)
0
 
knightEknightCommented:
did you change the onclick to include the return clause?

onclick="return myfunction();"
0
 
mrichmonAuthor Commented:
Oh I missed the return in the onclick both times.

That did the trick.

Thanks!
0
 
mrichmonAuthor Commented:
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

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now