Team LiB
Previous Section Next Section

Chapter 19: Creating Hyperlinked Ad Rotators

If you have experience working with Web pages, you may have noticed that there never seems to be enough room in the browser. With all those tables, images, frames, and text there's hardly any space left for the advertisements that you may depend on to keep your site running. While designing the Center Park Web site, imagine that over a dozen student organizations come to you requesting space on the main page of the site. Obviously, you can't accommodate all of them—at least not at the same time.

Displaying a Different Ad Each Time a Web Page Loads

Fortunately for you, Internet Explorer allows a Web page to change its content on-the-fly, through the use of the innerHTML property. You immediately started devising a way to rotate separate text advertisements each time a page loads. You can see how the code listing below is reflected in Figures 19.1 and 19.2, as the announcements are different between the two figures.

Click To expand
Figure 19.1: In this shot, we see the nerds getting a little publicity.
Click To expand
Figure 19.2: In this view, the advertisement has switched from the nerds to the school sports team.

As stated here, this example is for use only within Internet Explorer.


     <title>Center Park Home Page - Main Page</title>

     <script language="JavaScript"> 
          var ads = new Array( "Come watch the Center Park Bobcats!<br>Get your" + 
                               " schedules at the Union Building.", 
                               "Join us at the June Bug festival June 12-14th!" +  
                               "See you there.", 
                               "July 3rd is the first annual Adopt a Nerd" +  
                               " Day!<br>They are people too.<br>Be kind adopt" +  
                               " a nerd." );
          function getAd() 
            var ad = parseInt( Math.random() * ads.length ); 
            return( ads[ad] );

          function rotateAd() 
            document.all.adCell.innerHTML = getAd();

          function getCookieValue( name ) 
            var c = document.cookie; 
            var begin = c.indexOf( name ); 
            if( begin < 0 ) return( "" ); 
            begin += name.length + 1; 
            var end = c.indexOf( ";", begin ); 
            if( end == -1 ) end = c.length; 
            return( c.slice( begin, end ) );

          function setCustoms() 
            var bg = getCookieValue( "bgColor" ); 
            var fg = getCookieValue( "fgColor" ); 
            if( bg != "" ) document.bgColor = bg; 
            if( fg != "" ) document.fgColor = fg; 
        // -->

   <body onLoad="JavaScript: setCustoms(); rotateAd();">
     <font color="red" size="6"><b>
          Center Park Home Page 


      <table width="100%">
            <td ID="adCell" width="50%"> 
            <td width="50%">
              <a href="login.html">Log In</a><br>
              <a href="store.html">Store</a><br> 
              <a href="customize.html">Customize</a> 
          <td width="100%" colspan="2">
            <font face="Courier" size="5">*** School News ***</font> <br> 
            <b>July 4th</b> - Fireworks on the mall!&nbsp;  
            Come enjoy the holiday with the Center Park faculty,  
            staff and students.<br>
            <b>July 3rd</b> - First annual Adopt a Nerd Day!&nbsp;  
            They are people too.<br>
            <b>June 12th</b> - June Bug festival begins.


Each time it loads, this page will choose a random advertisement from a list of predefined advertisements and display it in the table.

Team LiB
Previous Section Next Section