admin管理员组

文章数量:1318973

I've placed an onClick goconfirm on a hyperlink per below:

<a href="" onclick="goConfirm('This link is to an external site. You are now 
leaving mysite.,'href=');' target="_blank">Online Account Opening</a>

How do I ensure the confirmation Javascript message fires before sending the user to the new site?

Thanks much for your help and guidance.

I've placed an onClick goconfirm on a hyperlink per below:

<a href="http://google." onclick="goConfirm('This link is to an external site. You are now 
leaving mysite.,'href=http://google.');' target="_blank">Online Account Opening</a>

How do I ensure the confirmation Javascript message fires before sending the user to the new site?

Thanks much for your help and guidance.

Share Improve this question asked Jan 10, 2013 at 3:34 SidCSidC 3,21315 gold badges74 silver badges139 bronze badges 3
  • Can you show the code in goConfirm? – Joseph Silber Commented Jan 10, 2013 at 3:35
  • 3 You're missing a ' at the end of your first argument. Additionally, you have a ' at the end of the attribute value instead of a ", which was used to start the value. – Sampson Commented Jan 10, 2013 at 3:35
  • 3 Check the console, please! – Ry- Commented Jan 10, 2013 at 3:36
Add a ment  | 

4 Answers 4

Reset to default 5

You are not preventing the default action in any way. Try this instead:

<a href="..." onclick="return confirm('This link is... ');">...</a>

Or if goConfirm is actually a function of yours, you should add return false; to the end of the onclick.

Also fix the mismatched quotes ;)

I guess it was a typo. Try this instead:

<a href="http://google." onclick="goConfirm('This link is to an external site. You are now leaving mysite.','href=http://google.');return false;" target="_blank">Online Account Opening</a>

I also added a return false at the end to ensure the current web page won't be redirected.

You should remove the code to open the new page from the anchor tag to the JS function so that it waits till you confirm.

<script>
function goConfirm(message, url) {
  var choice = confirm(message);
  if (choice) {
    window.open(url, "_blank");
  }
}
</script>
<a href="#" onclick="goConfirm('This link is to an external site. You are now leaving mysite.,\'href=http://google.\'', 'http://google.');">Online Account Opening</a>

Fiddle at - http://jsfiddle/poonia/2bYKV/

use <a href="javascript:void(0)" onclick="return confirm('This link is... ');">...</a> to avoid any change in url

本文标签: htmlJavascript onClick confirm not firingStack Overflow