2

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>
2
  • Works fine for me... Commented Mar 3, 2015 at 1:23
  • It worked for me in the beginning as well and all of a sudden stopped working. Commented Mar 4, 2015 at 13:31

2 Answers 2

5

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>
Sign up to request clarification or add additional context in comments.

Comments

-2

You are redirecting twice. Change it to:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.