最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Stop an anchor from loading on javascript confirm - Stack Overflow

matteradmin2PV0评论

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/?remove=yes" onclick="myconfirm('my_string')">My String</a> 

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./?remove=yes" onclick="myconfirm('my_string')">My String</a> 
Share Improve this question asked Apr 10, 2010 at 23:11 Joseph CarringtonJoseph Carrington 4511 gold badge7 silver badges22 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 9

You're not returning anything from the onclick handler. Do

onclick="return myconfirm('my_string');"

Also, you can shorten that if clause to just

return agree;

Or even just

return confirm('Are you sure you want to remove ' + my_string + '?');

Matti's right about the need to return something. However:

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

Don't use a link for this. Anything that has an active effect like a removal should be a form with method="post", not a form or link that causes GET, and the server-side script that does the removing should only accept POST requests.

Accepting GET for non-idempotent actions, as well as being against the standard, can lead to a variety of amusing problems.

You can style a form submit button so it doesn't look like a button, if you really want to:

<form class="confirm" method="post" action="/remove" title="remove my string"><div>
    <input type="submit" class="unbutton" value="My String" />
</div></form>

.unbutton {
    border: none; padding: 0;
    background: transparent; color: blue;
    text-decoration: underline; cursor: pointer;
}

You can also avoid having to use inline event handler attributes by assigning them from JS:

// Find forms with confirm class and bind to them
//
for (var i= document.forms.length; i-->0;)
    if (document.forms[i].className==='confirm')
        document.forms[i].onsubmit= getConfirmation;

// Prompt user to allow submission or not
//
function getConfirmation() {
    return confirm('Are you sure you wish to '+this.title+'?');
}

Return false in the onclick to stop the event.

onclick="if( !myconfirm('my_string') ) { return false; }"

Or simply:

onclick="return myconfirm('my_string');"

In onclick you should return value of myconfirm: onclick="return mycnfirm('my_string')". By the way, you could just return the result of confirm in your myconfirm:

 function myconfirm(my_string) {
      return confirm('Are you sure you want to remove ' + my_string + '?');
 }

Put "javascript:void(0)" as the href and then handle everything in the onclick, including the location.href change, if needed.

Post a comment

comment list (0)

  1. No comments so far