Javascript onClick确认没有解雇

时间:2013-01-10 03:34:15

标签: javascript html

我在下面的超链接上放置了一个onClick goconfirm:

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

如何确保在将用户发送到新网站之前触发确认Javascript消息?

非常感谢您的帮助和指导。

4 个答案:

答案 0 :(得分:5)

您没有以任何方式阻止默认操作。试试这个:

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

或者如果goConfirm实际上是您的功能,则应在onclick的末尾添加return false;

同时修复不匹配的引号;)

答案 1 :(得分:2)

我猜这是一个错字。试试这个:

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

我还在最后添加了return false,以确保不会重定向当前网页。

答案 2 :(得分:0)

您应该删除代码以从锚标记打开新页面到JS函数,以便它等待您确认。

<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.com\'', 'http://google.com');">Online Account Opening</a>

小提琴 - http://jsfiddle.net/poonia/2bYKV/

答案 3 :(得分:0)

使用<a href="javascript:void(0)" onclick="return confirm('This link is... ');">...</a>来避免更改网址

相关问题