How To Randomize Links?

Asked By 0 points N/A Posted on -
qa-featured

I am trying to create a simple personal website for mobile phones and I am testing scripts found on other websites to randomize the displaying of links but can’t make them work. I have several links that I want to display on the page one by one in random. How do I do that?

I want to display one link then when the page is refreshed or the user went to another page a different link will be shown.

SHARE
Answered By 590495 points N/A #346988

How To Randomize Links?

qa-featured

There are many different methods how to randomize links on websites depending on the platform you are using. For WordPress, I know the platform has its own method of displaying things like links in random order. For HTML pages, you can use a custom script to perform the randomization. You can search the internet for websites that offer methods to randomize things.

Here’s a script I use to display links in random:

function viewingAD() {
var footSet = new Array();
footSet[1] = “URL 1”;
footSet[2] = “URL 2”;
footSet[3] = “URL 3”;

var footSetTITLE = new Array();
footSetTITLE[1] = “Link text for URL 1”;
footSetTITLE[2] = “Link text for URL 2”;
footSetTITLE[3] = “Link text for URL 3″;

var min = 1;
var max = 3;
var get_seed = Math.floor(Math.random() * (max – min + 1)) + min;

document.write(‘<a href=’+'”‘+’javascript:void(0)’+'”‘+’ onclick=’+'”‘+’this.href=’+”’+footSet[get_seed]+”’+'”‘+’ target=’+'”‘+’_blank’+'”‘+’><strong>’+footSetTITLE[get_seed]+'</strong></a>’);
}

This script is good to go. Just replace “URL 1,” “URL 2,” and “URL 3” with the links or URLs you want to use then supply the correct or corresponding link title or text in “Link text for URL 1,” “Link text for URL 2,” and “Link text for URL 3.”

To use the script, create a JS file something like “random.js” then just copy and paste the script. Once you have the JS file, link it anywhere between the head tag:

<head>
<script type=”text/javascript” src=”https://url.com/random.js”></script>
</head>

Now, once you have the script linked on the page, you need to put this piece of code on the specific location where you want to display the random link:

<script type=”text/javascript”>viewingAD()</script>

I created the script so that when the link is displayed and clicked, the target page will open on a new tab preserving the current page where you’re at. What the script does is it generates a random number or integer anywhere between the minimum and maximum variable. The minimum variable is set by “var min = 1;” so don’t change this line.

The maximum variable is set by “var max = 3;” so you need to change this value as you add more links to the script. When you add more links to the script, make sure to add the corresponding title to avoid any errors on the script.

Related Questions