Persistent Random Banners: Selecting the Message | WebReference

Persistent Random Banners: Selecting the Message


Persistent Random Banners

Selecting the Message

Selecting the message is done in the function refreshDocJSTOD(). The frequency parameter is the frequency at which the page owner wants to refresh the banner content. We start by recording the feed's length:

var max = tips.length;

Then we create a date object from which we'll read all the current date information:

var dateObj = new Date();

The main portion of the function is occupied by a CASE statement that deals with the following frequency parameter values:

If the parameter does not match any of the above five alternatives, the CASE statement defaults to the "now" value.

For each match, we assign tipIndex with the relevant numeric value corresponding to the current date entry:

Once a numeric value is computed, we need to make sure it falls in the range of message numbers in the banner feed. A simple way to do it is by computing the remainder of the quotient:

tipIndex = tipIndex % max;

Now, tipIndex is both unique and in the proper message range. We can now display it:

document.write(tips[tipIndex]); 

Here is the whole refreshDocJSTOD() function:

function refreshDocJSTOD(frequency) {
  var max = tips.length;
  var dateObj = new Date();
  switch(frequency) {
    case "month": // 0 - 11
      tipIndex = dateObj.getMonth();
      break
    case "dayOfTheMonth": // 1 - 31
      tipIndex = dateObj.getDate() - 1 // 0 - 30
      break;
    case "dayOfTheWeek": // 0 - 6
      tipIndex = dateObj.getDay();
      break;
    case "hour": // 0 - 23
      tipIndex = dateObj.getHours();
      break;
    case "now": // Default
    default:
      tipIndex = getRandomIndex(max);
   }
 tipIndex = tipIndex % max;
 document.write(tips[tipIndex]);
}

Still left to be explained is the function getRandomIndex(). This function accepts as a parameter the range of messages in the banner feed (max), and returns a random number within this range. Here is its listing:

function getRandomIndex(max) {
 var randomNum = Math.random();
 randomNum = randomNum * max;
 randomNum = parseInt(randomNum);
 if(isNaN(randomNum)) randomNum = 0; // for Netscape
 return randomNum;
}

The function includes a standard procedure for computing random numbers within a given range. First, we get a random fraction between 0 and 1 by calling the intrinsic function, Math.random(). Then we multiply this fraction by the feed's size, randomNum = randomNum * max and then converting the result to an integer value, randomNum = parseInt(randomNum). Netscape Navigator has some difficulties in the above sequence, so if the computation does not yield a valid number (isNaN(randomNum)), a value of 0 is picked up. The function returns a persistent random number.

Next: How to build the message feed

https://www.internet.com

Produced by Yehuda Shiran and Tomer Shiran

Created: March 27, 2000
Revised: April 26, 2000

URL: https://www.webreference.com/js/column60/3.html