Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > Ajax and JavaScript

  • Print
  • + Share This
This chapter is from the book

Adding Links to Cycling Banners

Banners are often used in advertising, and you’ll want to know how to make a banner into a link that will take a visitor somewhere when the visitor clicks the banner. Listing 4.16 shows the HTML page, which differs from the last example only in that it adds a link around the img tag. Listing 4.17 shows a variation of the previous script. In this script, we’ll add a new array. This new array contains destinations that users will be sent to when they click the banner. In this case, the “Eat at Joe’s” banner takes you to, “Drink more Java” goes to, and “Heartburn” goes to, as shown in red-circle-a.jpg. No editorial comments implied, of course.

To add links to cycling banners:

  1. window.onload = initBannerLink;

    When the window finishes loading, trigger the initBannerLink() function.

    Listing 4.16. The HTML needed for an ad banner.

    <!DOCTYPE html>
         <title>Rotating Banner with Links</title>
         <script src="script08.js"></script>
         <link rel="stylesheet" href="script01.css">
         <div class="centered">
            <a href="linkPage.html"><img src="images/banner1.gif" id="adBanner" alt="ad banner"></a>
  2. if (document.getElementById("adBanner").parentNode.tagName == "A") {
       document.getElementById("adBanner").parentNode.onclick = newLocation;

    This code, inside the initBannerLink() function, first checks to see if the adBanner object is surrounded by a link tag. If so, when the link is clicked, the newLocation() function will be called. Finally, the rotate() function is called.

    Listing 4.17. This script shows how you can turn cycling banners into real, clickable ad banners.

    window.onload = initBannerLink;
    var thisAd = 0;
    function initBannerLink() {
         if (document.getElementById("adBanner").parentNode.tagName == "A") {
            document.getElementById("adBanner").parentNode.onclick = newLocation;
    function newLocation() {
         var adURL = new Array("","","");
         document.location.href = "http://www." + adURL[thisAd];
         return false;
    function rotate() {
         var adImages = new Array("images/banner1.gif","images/banner2.gif","images/banner3.gif");
         if (thisAd == adImages.length) {
            thisAd = 0;
         document.getElementById("adBanner").src = adImages[thisAd];
         setTimeout(rotate, 3 * 1000);
  3. function newLocation() {
       var adURL = new Array("","","");

    In the new function newLocation(), the adURL variable gets assigned the three constituents of a new array. Just the domain names need to go in here, because we’ll complete the URLs next.

  4. document.location.href = "http://www." + adURL[thisAd];
    return false;

    Still inside newLocation(), we set the document.location.href object (in other words, the current document window) to the value of the text string http://www. (notice the period), plus the value of one item from adURL. Since adURL is an array, you need to specify a member of the array. That’s stored in thisAd, and the resulting string can be any of the three links, depending on when the user clicks. Last, it returns false, which tells the browser that it should not also load in the href. Otherwise, the browser would do both. We’ve handled everything within JavaScript, so the href doesn’t need to be loaded.

  • + Share This
  • 🔖 Save To Your Account