Sponsorship Rotator (JavaScript Sample)
The following functions were written with advertising in mind. I was asked by a few people for an easy way to rotate banners without using server-side code. The obvious answer: JavaScript. I'm not a huge fan of extensive use of JavaScript on a site as it's incompatibility across browsers and client-side computation has the tendency to wreak havoc for careless webmasters. Still, it's hard to get good site interactivity without it.

/* All code (c) Copyright 2002 by Rob Zazueta
*
*  You are free to use this code provided you leave the copyright intact. 
*  I'd also appreciate an e-mail from you letting me know where and how 
*  you're using it, just to get a feeling for how much it is being used.
*/

var banners = null;
var urls = null;
var path = null;

//Assigns the proper arrays to this page
function Sponsor(banners, urls, path) {
	this.banners = banners;
	this.urls = urls;
	this.path = path;
}
var myBanners = null;
var myUrls = null;
var myPath = null;


var myBanners = new Array('model1.gif', 'model2.gif', 'model3.gif', 'gen2.gif', 'gen5.gif');
//var myUrls = new Array('http://signup.avsofchoice.com/apply?refws=5225063');
var myUrls = new Array('link1', 'link2', 'link3', 'link4', 'link5');
var myPath = '/isleofdelight/sponsors/cyberage/horizontal/';
var cyberage = new Sponsor(myBanners, myUrls, myPath);


var sponsors = new Array(cyberage);

/* The displayBanner code assumes the very first two links and the very first image on any page
*  will be the the sponsorship banner. On IOD, the site is designed specifically for this contingency.
*  However, names would be a bit more useful.
*/

function displayBanner() {
	var sponsorImg = new String();
	var sponsorUrl = new String();
	var sponsorSelect = Math.round((1000 * Math.random())) % sponsors.length;
	var sponsor = sponsors[sponsorSelect];
	var banners = sponsor.banners;
	var selection = Math.round((100 * Math.random())) % banners.length;

	if(sponsor.banners.length == sponsor.urls.length) {
		sponsorImg = sponsor.path + sponsor.banners[selection];
		sponsorUrl = sponsor.urls[selection];
	} else {
		sponsorImg = sponsor.path + sponsor.banners[selection];
		sponsorUrl = sponsor.urls[0];
	}

	document.bannerimg.src = sponsorImg;
	for (n=0; n < document.links.length; n++) {
		if(document.links[n].name == "banner") {
			document.links[n].href = sponsorUrl;
		}
	}
	//document.links[0].href = sponsorUrl;
	//document.links[1].href = sponsorUrl;
	return true;
}




Home · Photography · Brew Blog · Resume · Contact me
 
Creative Commons License
Unless otherwise noted, all content on this site is Copyright © 2004 by Rob Zazueta and licensed under a Creative Commons License.