How do I cancel a href in javascript?

I have a link like:

<a href=go_somewhere.htm onClick=doTest() return>Go</a>

I have JavaScript like:

function doTest(){
     return confirm("Click OK for next page, or click Cancel to stay on this page");
}

Using this construct it always goes to the next page regardless of which button is clicked in the JavaScript pop-up.
vogtsterAsked:
Who is Participating?
 
snoyes_jwCommented:
try
<a href='go_somewhere.htm' onClick='return doTest();'>
0
 
archrajanCommented:
<a href="#" onClick=doTest();return false;>Go</a>

I have JavaScript like:

function doTest(){
  var a =   return confirm("Click OK for next page, or click Cancel to stay on this page");
if(a)
window.location.href = "go_somewhere.htm"
else
return false;
}
0
 
archrajanCommented:
u shud remove the return in the var a line

see here this example

<BODY>
 <a href="#" onClick=doTest();return false;>Go</a>

I have JavaScript like:
<script>
function doTest(){
  var a =   confirm("Click OK for next page, or click Cancel to stay on this page");
if(a)
window.location.href = "http://www.yahoo.com"
else
return false;
}
</script>
</BODY>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.