Pass link to anchor from Javascript

Hi all

I’m newish to Javascript but I come from using Actionscript for years.

I have a simple a tag in html that when clicked I want to link to a random address picked from an array of possible links.

So in the code here I have an array of the links and I’m picking a random number from the length of that array, I’m then using that number to pick a random link from the array.

<script type="text/javascript">
		  var links_arr = ["","","",""]
      function link(){
        var ranNum = Math.floor(Math.random()*links_arr.length)
        var linkTo = links_arr[ranNum];

This all works fine and the window.alert show randomly picked links from the array.

The problem is passing the link to the href of the a tag - how can I use the link in the href ?

<a class="buy" href="javascript:link()" target="_blank">buy</a>

When the person clicks the link.

I can pick the link randomly, it was just passing it to the a tag

Do you want the link to get the random link before the person clicks on it, or do you want the random link to be chosen when the person clicks on the link?

Tip: the second option is a lot easier to do.

window.location maybe?

In that case you just want to set window.location to the new link.