September 27, 2000 - Randomizing a Link | WebReference

September 27, 2000 - Randomizing a Link

Yehuda Shiran September 27, 2000
Randomizing a Link
Tips: September 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

A good way to draw attention to your site is to surprise users with something new and different each time they visit your site. Displaying a random link is one way to achieve that effect. There are many techniques to pick a link or a message in random. Here is a simple script that will do the trick. It displays the following link (reload the tip to see a different link):

And here is the code:

<HTML>
<HEAD>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
var urlarray= new Array("http://www.hp.com/brio",
      "http://www.ibm.com",
      "http://www.acer.com",
      "http://www.toshiba.com");
var textarray= new Array("Brio Support Center",
      "IBM Aptiva Center",
      "Acer Technologies",
      "toshiba site");
randomno=Math.floor((Math.random()*urlarray.length));
urlstr="<a href=\""+urlarray[randomno]+"\">"
urlstr+=textarray[randomno];
urlstr+="</a>"
document.write (urlstr);
</SCRIPT>
</BODY>
</HTML>

This tip has been contributed by Pratim Guha Biswas.

Learn more about random banners in Column 60, Persistent Random Banners.