mattchambers
mattchambers

Reputation: 185

Javascript window.location.href onClick not working

So I have a basic html page and basic Javascript in the head. I'm trying to redirect using Javascript once the function is activated through onClick but window.location.href wont work. window.alert works and everything else.

HTML

<html>
<head>

<title>Testing Aff Links</title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script type="text/javascript">
function myfunc(){

    window.location.href="http://youtube.com";
}
</script>
</head>



<body>

Link <a href="http://google.com" class="link" onClick="myfunc()">Link #1</a>

  </body>
      </html>

Upvotes: 2

Views: 5112

Answers (2)

Barmar
Barmar

Reputation: 780843

You need to return false from the onclick attribute to prevent the default action:

Link <a href="http://google.com" class="link" onClick="myfunc(); return false;">Link #1</a>

Upvotes: 5

nanndoj
nanndoj

Reputation: 6770

You are redirecting twice. Change it to:

Link <a href="javascript:;" class="link" onClick="myfunc()">Link #1</a>

Upvotes: -2

Related Questions