Stop an anchor from loading on javascript confirm

Posted by Joseph Carrington on Stack Overflow See other posts from Stack Overflow or by Joseph Carrington
Published on 2010-04-10T23:11:01Z Indexed on 2010/04/10 23:13 UTC
Read the original article Hit count: 365

Filed under:

I was under the impression that this was formed correctly, but here it is forwarding to the anchor href (clicking through? what should I call this?) whether or not the user selects cancel or okay.

<script type="text/javascript">
function myconfirm(my_string)                                                 
{                       
 var agree = confirm('Are you sure you want to remove ' + my_string + '?');
 if(agree)                                                                       
 {                                                                               
  return true;                                                            
 }       
 else                                                                            
 {                                                                               
  return false;
 }
}                                                                                       
</script> 

and the anchor

<a href="example.com/?remove=yes" onclick="myconfirm('my_string')">My String</a> 

© Stack Overflow or respective owner

Related posts about JavaScript