Button that links to random external sites on click


#1

Hi,

I’m trying to make a button which links to a set of different external links, lets say about 10. Each time you click the button, it takes you to one of these sites at random.

I don’t know if this is the best way to do it but this is how I have been attempting it by:
Creating a rectangle and then using the on mouse click run javascript function.

I’m kind of lost as to how to write the function to work correctly: (I found this code online and have been trying to alter it to work)

var links = new Array();
links[0] = "http://www.google.com/";
links[1] = "http://www.altavista.com/";
links[2] = "http://www.yahoo.com/";
links[3] = "http://www.excite.com/";

function untitledFunction1() {
  // Chooses a random link:
  var i = Math.floor(Math.random() * links.length);
  // Directs the browser to the chosen target:
  parent.location = links[i];
  return false;
}
//-->

Is it possible to do what I am asking? Am I using the function even slightly correctly? If anyone can push me in the right direction I would be very appreciative!

Thanks for your time.


(Steve Warby) #2

Hi that works fine.

If you add this line
console.log(’ link = ’ + links[i]);
and remark the parent.location = links[i];

Before pressing the button right click and select inspect element.
Then select the console.

Press the button.
You will see the result in the window.

This allows you to see what the results are before actioning anything.
Hope this helps.

Cheers

Steve Warby


#3

Hey Steve, thanks for the reply.

I’ve had a play around with what you posted, and the console tells me
"Error in untitledFunction9: ReferenceError: i is not defined"

I guess I haven’t correctly defined that the links[i]; isn’t referencing the various links. I’m not sure how to fix this error, any suggestions?


#4

As @classic12 said the code is fine and it will work. However, You have to place a call to the function for it to run. Just add

untitledFunction1();

after the code and it will run.

Also, the error you posted means that the variable i has not been defined. You have probably tried to use parent.location = links[i] and not assed the random link generator code above it.

If you share your document we can see what you are actually doing and be able to advise better :wink:


#5

randomlink.zip (14.4 KB)

Hi DBear, thanks for trying to help, I think that must be the issue.
I put the part I was working on into a new document and attached, as the original is quite large.


#6

Just get rid of the highlighted line and you will be good to go. The reason for the error is that you are trying to set the parent.location with a variable (i) that hasn’t been declared yet (it is declared in the function)


#7

Hi,

I just tried deleting that line of code and it didn’t change anything. I still got the same error message (in the console) and the rectangle didn’t open any of the links once clicked.


#8

sorry the console line has to go to or you can move it into the function as that is referencing the i variable too


#9

Amazing, thank you DBear and classic12 so much for the help!!! I really appreciate it :smiley:

This is what the final code looked like for anyone else if it helps:

var links = new Array();
links[0] = “http://www.google.com/”;
links[1] = “http://www.altavista.com/”;
links[2] = “http://www.yahoo.com/”;
links[3] = “http://www.excite.com/”;

function untitledFunction() {
// Chooses a random link:
var i = Math.floor(Math.random() * links.length);
// Directs the browser to the chosen target:
parent.location = links[i];
return false;

}
untitledFunction();