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

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

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.


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business