Nothing is for sale here. Freewill tips keep the site running. Want to help? → Tip via Paypal

Random Link Script

Could you use a script that automatically rotates links and descriptions each time the page loads? Here’s an example:
Recommended Resource

If you refresh the page you’ll probably see a different link. I say "probably" because it is truly random, so there will be times when the same link is shown twice in a row. I have a few links to my content and a few offsite links for this demo, but there aren't very many in total. The more links you have the less likely anyone will see the same link twice.

You can style the display any way you want. Once you have this set up it looks to visitors like you're updating your site regularly—even if you haven't been online for weeks! It takes a little time to put the links and descriptions together, but it's easy to set up. There are dozens of ways you could use this script. Need some ideas?

You can include as many links as you like in the script. There are two parts to it. The first part, shown in the black code box below, goes into an external Javascript file. Expand the instructions if you don't know how to do that.

Here's the script, with a copy button below it:

// specify the number of random links
var LinkSum = 3;

var url = new Array(LinkSum);
var description = new Array(LinkSum);
var anchorText = new Array(LinkSum);

url[0] = "https://www.someDomain.com";
description[0] = "Your description here.";
anchorText[0] = "Link Text Here";

url[1] = "https://anotherDomain.com";
description[1] = "This site's description here.";
anchorText[1] = "Link Text";

url[2] = "http://thirdDomain.com";
description[2] = "Description for this site here.";
anchorText[2] = "Link Text";

var RandomCrumb = Math.floor(Math.random() * LinkSum);

Four things to note:

  1. Line two reads: var LinkSum = 3;. That's the number of random links you have. If you add 15 links and don't adjust this number the script will only use the first three. This number should always be one more than the actual number of links you have because the link counter starts at zero instead of one. That's just the way arrays work in Javascript.
  2. Do not include any double quotation marks in the site descriptions or it will break the script, unless you escape them. That means if you use a double quotation mark it must preceded by a backslash, like this: \"   ...that tells the browser to print the character rather than interpret it as code.   Example: Joe \"Happy Guy\" Jones.
  3. You can use basic HTML formatting in the link descriptions in the .js file. Bold, italics, etc. CSS won't work. Remember not to use any double quotation marks unless you escape them as shown in the previous item!
  4. If you're linking to pages on your own site you don't have to include the full web address. Linking to: myPage.html will load your internal links faster than: http://www.myDomain.com/myPage.html because the latter link sends the browser back out to a domain name server to find your site all over again. The first link tells the browsers it's already on the desired site, just go to that page. If the page is in a different directory you'll have to include the correct path.

To add more random links, just copy one grouping and paste it into the script after the last grouping and edit it. Remember to change the numbers in the square brackets in all three lines of each grouping, as well as the LinkSum number at the start of the script.

reminder When you change the LinkSum number in line 2, it’s always one number greater than the numbers in your last link grouping because the Javascript array must start with 0 (zero) rather than 1.


The Easy Part

Now for the easy part. Just copy the code below and paste it into the spot in your page where you want the link and description to appear.
<script>
  var cont = "<a href='" + url[RandomCrumb] + "' target='_blank'>";
  cont += anchorText[RandomCrumb] + "
"; cont += description[RandomCrumb]; document.write(cont); </script>

As shown, the script will open links in a new window or tab. If you're using it for links within your own site you might want to change that so links open in the same window or tab. To change it, change line 2 to this;

var cont = "<a href='" + url[RandomCrumb] + "'>";

You can place the on-page part of the script in a paragraph, division, or anywhere you'd put text. You can style the container however you like to "dress up" the presentation. Have fun, good luck!