In this article we will see how to make a link that takes us to any URL. The address of the link will be selected randomly, among the elements of an array.
This is one of the first Javascript workshops that we wrote, with an effect that was quite typical in web pages before, creating a link that will randomly direct you to any page, among various alternatives that we can define.
For this we will use the Javascript language that will be executed in the browser. It’s going to be extremely simple, so it’s a good workshop for beginners and we think it will be instructive for readers.
Create the link array
To begin with, we are going to create an array with the different sites where our link could lead us. We have to define this array, logically, within a
In this way we have achieved two improvements:
- The link is perfectly valid. If Javascript didn't work, at least it would take us somewhere, which won't be random, because it's what is marked in the href, but at least it will be something.
- We've separated the Javascript code into a separate Random Link