spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column60


Persistent Random Banners

Developer News
Mozilla's Ubquity Mashup: For The Masses?
iPhone Users Just Want to Have Fun
Oops! I Fixed the Linux Kernel

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:

  • "month"
  • "dayOfTheMonth"
  • "dayOfTheWeek"
  • "hour"
  • "now"

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:

  • "month": a value between 0 and 11 is obtained by dateObj.getMonth().
  • "dayOfTheMonth": a value between 0 and 30 is achieved by dateObj.getDate() - 1.
  • "dayOfTheWeek": a value between 0 and 6 is computed by dateObj.getDay().
  • "hour": a value between 0 and 23 is found by dateObj.getHours().
  • "now": a true random number is computed by calling getRandomIndex(max). This function is described below.

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

http://www.internet.com

Produced by Yehuda Shiran and Tomer Shiran


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

webref The latest from WebReference.com Browse >
Performance Optimizations for High Speed JavaScript · Advanced Web Performance Optimization · Simple Comments Meets OpenID
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Extending Telephony: VoIP Call Recording for Business · U-Verse for Business Has Wi-Fi Perks · Lian-Li Launches New Power Supply Line, Rack Mount Kit and Fan Blower


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

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