Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / Javascript
Article

Banner.js

Rate me:
Please Sign up or sign in to vote.
3.00/5 (4 votes)
14 Oct 2003 87K   9   8
An object oriented approach for displaying banners.

Introduction

I was looking for a JavaScript banner solution that allows me to run banners on my web, when I discovered the BarelyFitz JavaScript slideshow. It's the best slideshow script I've seen, and I decided to use if for my banners.

Inheritance

The easiest method to "borrow" somebody else's code, is through inheritance. Using Nicholas C. Zakas's technique makes inheritance in JavaScript a dream:

JavaScript
Object.prototype.extend = function (oSuper) { 
       for (sProperty in oSuper) { 
               this[sProperty] = oSuper[sProperty]; 
       } 
}

function ClassA () { 
} 

function ClassB() { 
       this.extend(new ClassA()); 
}

The Banner Class

The reason I wrote this banner class, was to be able to have two (or more) banners on one page. The banner class acts as a wrapper around the slideshow class and inherits slideshow's methods and properties.

You'll find a demo here.

Prerequisite

BarelyFitz JavaScript slideshow.js.

The code

JavaScript
<HEAD>
<SCRIPT type="text/javascript" src="slideshow.js"></SCRIPT>
<SCRIPT language="JavaScript1.3" type="text/javascript">
<!--//

// From "<A href="http://www.sitepoint.com/article/1194" target=_blank>Rethinking JavaScript Objects</A>"
// makes it easy to inherit/extend an object
Object.prototype.extend = function (oSuper) { 
       for (sProperty in oSuper) { 
               this[sProperty] = oSuper[sProperty]; 
       }
}

// Banner(), a slideshow wrapper
// syntax:
//      var oVar = new Banner("oVar",url|urlarry [,target])
//      Note: oVar must be the same as the first parameter to Banner - "oVar"
//            the third parameter - target - is optional      
//            see <A href="#sample">sample use</A>
function Banner (id,url) { 
    //inherit methods and properties from slideshow()
    this.extend(new slideshow()); 

    //set inherited property 'name', required ref <A href="http://www.barelyfitz.com/webapps/apps/slideshow/index.php/5" target=_blank>slideshow documentation</A>
    this.name = id;

    //set target if defined
    if (arguments[2]) 
        this.target = arguments[2];
    else
        this.target = '_self';
    
    // images(), local method that adds images to the object
    this.images = function() {
        for(i=0;i<arguments.length;i++){ 
            // Create a slide
            s = new slide();
            s.src =  arguments[i];
            if (typeof(url) == 'string')
                s.link = url;
            else
                s.link = url[i];
            s.target = this.target;
            
            // inherited method, see <A href="http://www.barelyfitz.com/webapps/apps/slideshow/index.php/5" target=_blank>slideshow documentation</A>
            this.add_slide(s);
        }    
     }    
}


<A name=sample></A>
// create the first banner object, all images points to the same location

var firstBanner = new Banner("firstBanner","http://soderlind.no/","_blank");

 
// create the second banner object, each image points to a different location

var arrURLs = new Array(
     "http://www.codeproject.com"
    ,"http://soderlind.no/"
    ,"http://syndicated.INFO/"
);
var secondBanner = new Banner("secondBanner",arrURLs,"_blank");


// onload, init the banner objects and start them

function initBanner() {
    if (document.images) {
        //
        // firstBanner
        //
        firstBanner.images(
             "http://soderlind.no/images/soderlind468x60.gif"
            ,"http://soderlind.no/images/soderlind468x60-black.gif"
        );
        // image, timeout, update() and play() are inherited,
        // see <A href="http://www.barelyfitz.com/webapps/apps/slideshow/index.php/5" target=_blank>slideshow documentation</A>
        firstBanner.image = document.images.BANNER1;
        firstBanner.timeout = 5000;
        firstBanner.update();
        firstBanner.play();
        //
        // secondBanner
        //
        secondBanner.images(
             "http://www.codeproject.com/images/codeproject468x60.gif"
            ,"http://soderlind.no/images/soderlind468x60.gif"
            ,"http://syndicated.info/i/gfx/logo.gif"
        );
        secondBanner.image = document.images.BANNER2;
        secondBanner.timeout = 7000;
        secondBanner.update();
        secondBanner.play();
    }
}
//-->
</SCRIPT>
</HEAD>
<BODY onLoad="initBanner();">

<!-- firstBanner.hotlink() is inherited, see <A href="http://www.barelyfitz.com/webapps/apps/slideshow/index.php/5" target=_blank>slideshow documentation</A> -->
<A href="javascript:void(0)" onClick="firstBanner.hotlink();return false;">
    <IMG name="BANNER1" src="http://soderlind.no/images/soderlind468x60.gif" 
    width="468" height="60" border="0">
</A>

<A href="javascript:void(0)" onClick="secondBanner.hotlink();return false;">
  <IMG name="BANNER2" 
  src="http://www.codeproject.com/images/codeproject468x60.gif"  
  width="468" height="60" border="0">
</A>
</BODY>

Have fun coding.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Norway Norway
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralNightmare, not a dream Pin
darkoverlordofdata7-Jun-04 3:31
darkoverlordofdata7-Jun-04 3:31 
GeneralAdRotator Pin
Adrian Pirvu9-Oct-03 23:17
Adrian Pirvu9-Oct-03 23:17 
GeneralRe: AdRotator Pin
Uwe Keim10-Oct-03 2:25
sitebuilderUwe Keim10-Oct-03 2:25 
GeneralRe: AdRotator Pin
Adrian Pirvu10-Oct-03 3:03
Adrian Pirvu10-Oct-03 3:03 
GeneralRe: AdRotator Pin
Uwe Keim10-Oct-03 3:11
sitebuilderUwe Keim10-Oct-03 3:11 
GeneralRe: AdRotator Pin
John Kendrick18-Oct-03 10:36
John Kendrick18-Oct-03 10:36 
GeneralRe: AdRotator Pin
Per Søderlind18-Oct-03 15:46
sussPer Søderlind18-Oct-03 15:46 
GeneralRe: AdRotator Pin
John Kendrick19-Oct-03 11:05
John Kendrick19-Oct-03 11:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.