May 28, 2000 - Implementing Moving Ads | WebReference

May 28, 2000 - Implementing Moving Ads

Yehuda Shiran May 28, 2000
Implementing Moving Ads
Tips: May 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

If you are experiencing a low click-through rates, you may want to try a moving ad. Click the following button for an example:

The preceding button calls the following function:

function makeAd() {
  window.open("http://www.webreference.com/js/tutorial1/adpage.html", "ad", "width=468,innerWidth=468,height=80,innerHeight=80,left=0,top=0");
}

Here's the script in //www.webreference.com/js/tutorial1/adpage.html:

<SCRIPT LANGUAGE="JavaScript">
<!--
function startAd() {
  if (window.screen) {
    pos = 0;
    aw = screen.availWidth;
    window.moveTo(pos, 0);
    timerID = setInterval("moveAd()", 50);
  }
}
function moveAd() {
  if (pos <= 0) inc = 5;
  // 5 - so it does not pass the right edge
  // 10 - accounts for the window chrome
  if (pos + 468 + 10 + 5 > aw) inc = -5;
  pos += inc;
  window.moveTo(pos, 0);
}
window.onload = startAd;
// -->
</SCRIPT>

When adpage.html loads, the startAd() function is invoked. It only moves the window if the user's browser supports the window.screen object, which we need in order to calculate the width of the screen. The window slides at the top edge of the screen, from the upper-left corner (pos = 0) to the upper-right one.

We move the advertisement window 5 pixels every 50 milliseconds via the built-in setInterval() function. If you want to implement a "stop" button, it should run the following statement:

clearInterval(timerID);

Learn more on windows manipulation in Tutorial 1, Working with Windows.