DHTML Lab: Dynamic Headline Fader; The JavaScript Array | WebReference

DHTML Lab: Dynamic Headline Fader; The JavaScript Array


Dynamic Headline Fader
get your headlines ready

DHTML News Fader

Parameters used in this example:

• box width: 130;
• box height: 40;
• box color: white;

• blend interval: 2;
• blend duration: .25;
• loops: 1;

Link Text:
• text decoration: none;
• text alignment: left;
• font color: black;
• font size: 8pt;
• font weight: bold;
• font style: normal;
• font family: Arial,Geneva,sans-serif;
• line height: 9pt;

The headline fader can of course display simple static headlines or live linked headlines. Our example demonstrates the latter. All the examples on the left of every page use the same external source file to access the headlines. This external JavaScript file, which we call news.js, looks like this:

arURL = new Array(
arTXT = new Array(
  "High-Speed Hotel Access on the Horizon",
  "Excite Reports Increased Revenues",
  "Virtual Reality E-Shopping Site Launches",
  "Microsoft May Face New Antitrust Case",
  "Ameritech in Compaq High-Speed Modem Deal",
  "Microsoft Declines Comment on BT Rumors",
  "Integrion Says Three Banks Test Electronic System",
  "Silicon Graphics Posts $31M Loss",
  "N2K, StarMedia in Lat. Amer. E-Commerce Initiative",
  "CitX, Priority One Launch Net Payment System",
  "MESA Suite Brings Self-Service to the Enterprise",
  "Snowbound Releases Raster Imaging Development Tool",
  "Net Shepherd Signs Revenue Deal With AltaVista"

Nothing more than two simple one-dimensional JavaScript arrays. The first, arURL, contains the URLs that should be used in the HREF attribute of the A tag. The second, arText, contains the actual headline text to be displayed.

These headlines are actual internet news headlines from Mecklermedia's internetnews.com. They all have the same base URL: http://www.internetnews.com/, so to avoid entering it each time, we declare a variable, prefix, to store it. Later, we will append it to each element of arURL. The prefix variable, therefore, is more-or-less a JavaScript equivalent of the BASE HREF= tag. If, in your custom fader, the URLs are from different servers, the prefix declaration may be omitted, or the statement changed to:

prefix = "";

if you want to keep the same code structure as our example. Don't forget that without a prefix, all elements of arURL must point to valid URLs, whether absolute or relative.

If, conversely, you want static, non-linked headlines, use only the arText array.

Whatever you decide, your HTML page must load the external file:

<SCRIPT LANGUAGE="JavaScript1.2"

The in-page SCRIPT

The first task of our in-page script is to create a single two-dimensional array from the arrays in the external script. This new array, which we will reference when building the fader, is called arNews:

<SCRIPT LANGUAGE="JavaScript1.2">
arNews = new Array();
for (i=0; i<arURL.length; i++) {
    arNews[i] = new Array();
    arNews[i][0] = prefix + arURL[i];
    arNews[i][1] = arTXT[i];

Every element of arNews has two elements of its own: the complete URL and the headline. The complete URL is created by combining prefix with the elements of arURL. If prefix has a value of "", just the arURL element is included.

We now have a two-dimensional array containing all the information we need for updating the fader. Next, we have to create the fader.

Produced by Peter Belesis and

All Rights Reserved. Legal Notices.
Created: Jan. 28, 1998
Revised: Feb. 03, 1998

URL: http://www.webreference.com/dhtml/column13/fadeArray.html