Sean's Personal Code Samples And References

Javascript Ad Rotator

ASP.NET has it's own but it does have its issues.

As with most drag and drop controls there is a lot of unneeded code
    added, increasing the page size and subsequent load times.
    
I use this on sites I build.

Here is the div to display the ads.

Put the div where you want to ad to display on the page.
<div id="divRandomBannerAd" />

Here is the client side script.

/*A 2 DIMENTIONAL ARRY TO HOLD 
    1. URL WHERE A CLICK ON THE ADD WILL TAKE YOU
    2. BANNER IMAGE PATH
    3. ALT TEXT FOR THE BANNER IMAGE*/
var arrBannerAds = new Array();

The slickest way to do this is right out of a Database.

//THE FIRST AD STARTS WITH ZERO
arrBannerAds[0] = new Array();
arrBannerAds[0][0] = 'The Url to open when the ad is clicked.'
arrBannerAds[0][1] = 'The Url to the ad image being displayed on the page'
arrBannerAds[0][2] = 'Some alt text to go with the image'

//JUST KEEP ADDING TO THE ARRAY INCREASING THE INDEX FOR EACH AD
arrBannerAds[1] = new Array();
arrBannerAds[1][0] = 'The Url to open when the ad is clicked.'
arrBannerAds[1][1] = 'The Url to the ad image being displayed on the page'
arrBannerAds[1][2] = 'Some alt text to go with the image'

//NEED A WINDOW LOAD EVENT TO GET THINGS STARTED
window.onload=function(){
    
    //START THE RANDOM AD TIMER
	BannerAdTimmer();
	
	//THERE CAN ONLY BE ONE WINDOW LOAD EVENT PER PAGE
	//SINCE I RUN THIS ON MY MASTER PAGE AND SOMETIMES 
	//I WANT A WINDOW LOAD EVENT TO USE ON A SPECIFIC PAGE
	//I USE THIS jsPageLoad() ON INDIVIDUAL PAGES
	//TO RUN WHAT EVER SCRIPT I NEED ON THAT PAGE
	try{jsPageLoad();}catch(err){}
}

//RANDOM AD TIMER
function BannerAdTimmer(){

    //FUNCTION CALL TO DISPLAY THE AD
	RandomBannerAd();

	//CALL THIS FUNCTION AGAIN IN 6 SECONDS
	setTimeout("BannerAdTimmer()",6000);//6 SECONDS
}

//GLOBAL HYPERLINK TO APPEND THE ADD TO
//SO THE PREVIOUSE CAN BE REMOVED BEFORE THE NEXT IS APPENDED
var hypBannerAd;

//SET COUNT TO RANDOM NUMBER FOR THE FIRST AD TO DISPLAY
//THEN IT WILL CONTINUE TO JUST LOOP THROUGH EACH AD IN THE ARRAY
var cntBannerAd = Math.floor(Math.random() * arrBannerAds.length);

//RANDOM ADD FUNCTIONS
function RandomBannerAd(){
    try{
        //REFERENCE TO THE DIV DISPLAYING THE ADD
        var divAd = document.getElementById('divRandomBannerAd');
        
        //REMOVE ANY PREVOUSE AD
        try{divAd.removeChild(hypBannerAd);}
        catch(err){}
        
        //CREATE THE IMAGE OBJECT (THE AD TO DISPLAY)
        var imgBannerAd = new Image();
        
        //SET THE SOURCE OF THE IMAGE FROM THE ARRAY
        imgBannerAd.src = arrBannerAds[cntBannerAd][1];
        
        //SET THE CLASS USED ON THE AD TO REMOVE THE BORDER ECT...
        imgBannerAd.className = 'bannerAd';
        
        //CREATE THE HYPER LINK TO OPEN THE URL WHEN THE AD IS CLICKED
        hypBannerAd = document.createElement('a');
        
        //SET THE URL OF THE HYPER LINK FROM THE ARRY
        hypBannerAd.href = arrBannerAds[cntBannerAd][0];
        
        //OPEN THE URL IN A NEW WINDOW SO THE USER IS NOT LEAVING THE SITE
        hypBannerAd.target = '_blank'
        
        //APPEND THE AD IMAGE TO THE HYPER LINK
        hypBannerAd.appendChild(imgBannerAd);
        
        //APPEND THE HYPER LINK TO THE DIV DISPLAYING THE AD
        divAd.appendChild(hypBannerAd);
        
        //INCREMENT THE COUNTER SO NEXT TIME THE NEXT AD WILL DISPLAY
        if (cntBannerAd < arrBannerAds.length -1)
        cntBannerAd += 1;
        else //THIS IS THE LAST AD SO SET THE COUNT BACK TO ZERO
        cntBannerAd = 0
        
        //SET THE ALT AND TITLE ATTRIBUTES FOR THE AD
        imgBannerAd.alt = arrBannerAds[cntBannerAd][2];
        imgBannerAd.title = arrBannerAds[cntBannerAd][2];
        
        //ALL FUNCTIONS SHOULD RETURN A VALUE
        return true;
        
    //CATCH ANY ERRORS THAT MAY OCCUR    
    }catch(err){}
}
Sean Marcellus
There are 10 kinds of people e in this world, those who understand binary and those who don’t.