Current location: Hot Scripts Forums » General Web Coding » JavaScript » Ad banner rotator randomization


Ad banner rotator randomization

Reply
  #1 (permalink)  
Old 08-24-05, 11:30 AM
Anessa Anessa is offline
New Member
 
Join Date: Aug 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Ad banner rotator randomization

Hello,

I'm using 'mbanner' to rotate Flash and animated gif ad banners on my website's homepage. It works great! However; my boss came to me to say that most of the advertisers whose banners are up are mad because the same banner always appears first. So to be fair, I need to make the banner ads appear in random order, making sure that a different banner appears whenever the visitor returns to the homepage. I found this banner script by Manish Kumar Namdeo, but I cannot locate the author to ask how to randomize the banners. Below is the javascript and below that is the piece of code on the homepage that works with the javascript. If anyone can help me edit the javascript so as to make the banners appear in random order, I would really appreciate it. Thanks in advance! Anessa.........
Code:
/////////////////////////////////
// File Name: mBanner.js       //
// By: Manish Kumar Namdeo     //
/////////////////////////////////

// BANNER OBJECT
function Banner(objName){
	this.obj = objName;
	this.aNodes = [];
	this.currentBanner = 0;
	
};

// ADD NEW BANNER
Banner.prototype.add = function(bannerType, bannerPath, bannerDuration, height, width, hyperlink) {
	this.aNodes[this.aNodes.length] = new Node(this.obj +"_"+ this.aNodes.length, bannerType, bannerPath, bannerDuration, height, width, hyperlink);
};

// Node object
function Node(name, bannerType, bannerPath, bannerDuration, height, width, hyperlink) {
	this.name = name;
	this.bannerType = bannerType;
	this.bannerPath = bannerPath;
	this.bannerDuration = bannerDuration;
	this.height = height
	this.width = width;
	this.hyperlink= hyperlink;
//	alert (name +"|" + bannerType +"|" + bannerPath +"|" + bannerDuration +"|" + height +"|" + width + "|" + hyperlink);
};

// Outputs the banner to the page
Banner.prototype.toString = function() {
	var str = ""
	for (var iCtr=0; iCtr < this.aNodes.length; iCtr++){
		str = str + '<span name="'+this.aNodes[iCtr].name+'" '
		str = str + 'id="'+this.aNodes[iCtr].name+'" ';
		str = str + 'class="m_banner_hide" ';
		str = str + 'bgcolor="#FFFCDA" ';	// CHANGE BANNER COLOR HERE
		str = str + 'align="center" ';
		str = str + 'valign="top" >\n';
		if (this.aNodes[iCtr].hyperlink != ""){
			str = str + '<a href="'+this.aNodes[iCtr].hyperlink+'">';
		}
			
		if ( this.aNodes[iCtr].bannerType == "FLASH" ){
			str = str + '<OBJECT '
			str = str + 'classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '
			str = str + 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" '
			str = str + 'WIDTH="'+this.aNodes[iCtr].width+'" '
			str = str + 'HEIGHT="'+this.aNodes[iCtr].height+'" '
			str = str + 'id="bnr_'+this.aNodes[iCtr].name+'" '
			str = str + 'ALIGN="" '
			str = str + 'VIEWASTEXT>'
			str = str + '<PARAM NAME=movie VALUE="'+ this.aNodes[iCtr].bannerPath + '">'
			str = str + '<PARAM NAME=quality VALUE=high>'
			str = str + '<PARAM NAME=bgcolor VALUE=#FFFCDA>'
			str = str + '<EMBED ';
			str = str + 'src="'+this.aNodes[iCtr].bannerPath+'" '
			str = str + 'quality=high '
//			str = str + 'bgcolor=#FFFCDA '
			str = str + 'WIDTH="'+this.aNodes[iCtr].width+'" '
			str = str + 'HEIGHT="'+this.aNodes[iCtr].height+'" '
			str = str + 'NAME="bnr_'+this.aNodes[iCtr].name+'" '
			str = str + 'ALIGN="center" '
			str = str + 'TYPE="application/x-shockwave-flash" '
			str = str + 'PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">'
			str = str + '</EMBED>'
			str = str + '</OBJECT>'
		}else if ( this.aNodes[iCtr].bannerType == "IMAGE" ){
			str = str + '<img src="'+this.aNodes[iCtr].bannerPath+'" ';
			str = str + 'border="0" ';
			str = str + 'height="'+this.aNodes[iCtr].height+'" ';
			str = str + 'width="'+this.aNodes[iCtr].width+'">';
		}

		if (this.aNodes[iCtr].hyperlink != ""){
			str = str + '</a>';
		}

		str += '</span>';
	}
	return str;
};

// START THE BANNER ROTATION
Banner.prototype.start = function(){
	this.changeBanner();
	var thisBannerObj = this.obj;
	// CURRENT BANNER IS ALREADY INCREMENTED IN cahngeBanner() FUNCTION
	setTimeout(thisBannerObj+".start()", this.aNodes[this.currentBanner].bannerDuration * 5000);
}

// CHANGE BANNER
Banner.prototype.changeBanner = function(){
	var thisBanner;
	var prevBanner = -1;
	if (this.currentBanner < this.aNodes.length ){
		thisBanner = this.currentBanner;
		if (this.aNodes.length > 1){
			if ( thisBanner > 0 ){
				prevBanner = thisBanner - 1;
			}else{
				prevBanner = this.aNodes.length-1;
			}
		}
		if (this.currentBanner < this.aNodes.length - 1){
			this.currentBanner = this.currentBanner + 1;
		}else{
			this.currentBanner = 0;
		}
	}
	

	if (prevBanner >= 0){
		document.getElementById(this.aNodes[prevBanner].name).className = "m_banner_hide";
	}
	document.getElementById(this.aNodes[thisBanner].name).className = "m_banner_show";
}

-----------------------------------------------------------
<!--NEW BANNER AD ROTATION-->
<script language="javascript">
                    banner1 = new Banner('banner1');
                    banner1.add("FLASH", "images/ads/banners/Final_white_468x60_ver4.swf", 10, 60, 468,"http://www.actuarialcareers.com");
					banner1.add("IMAGE", "images/ads/andover2.gif", 10, 60, 468,"http://www.andoverresearch.com");
                    banner1.add("IMAGE", "images/ads/milliman_banner_revised3a.gif", 10, 60, 468,"http://www.actuarialsoftware.com");
					banner1.add("IMAGE", "images/ads/pryor_long.gif", 10, 60, 468,"http://www.ppryor.com/actuaria.htm");
                    document.write(banner1);
                    banner1.start();
</script>
<!--END NEW BANNER AD ROTATION-->

Last edited by TwoD; 04-06-06 at 05:13 PM.
Reply With Quote
  #2 (permalink)  
Old 08-25-05, 04:33 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
Change the changeBanner function to this:
Code:
// CHANGE BANNER
Banner.prototype.changeBanner = function(){
 var oldBanner=this.currentBanner
 do{
  this.currentBanner=this.aNodes[Math.round((this.aNodes.length-1)*Math.random())]
 }while(this.currentBanner!=oldBanner)

 if (document.getElementById(this.aNodes[oldBanner].name).className=='m_banner_show'){
  document.getElementById(this.aNodes[oldBanner].name).className = "m_banner_hide";
 }
 document.getElementById(this.aNodes[this.currentBanner].name).className = "m_banner_show";
}
This will show banners completely randomly which means any banner may come up any number of times. It atleast makes sure it's not displaying the same banner twice in a row. I could extend it further by giving it a "memory" of which banners have previosly been shown, so it won't display one banner twice before all other banners have been shown.
Note: I haven't tested the code, there might be errors.
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
  #3 (permalink)  
Old 10-22-05, 12:48 PM
ClubKahuna ClubKahuna is offline
New Member
 
Join Date: Oct 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Modifying mBanner.js

Hello,

I am also tasked to modify this script by adding two functions:

1. Previous & Next buttons (So you can advance or backtrack banners quickly)
2. Change the transition effect to Blinds

Can you assist by supplying the code to accomplish this? I am just learning JS.

Thanks!
JB
Reply With Quote
  #4 (permalink)  
Old 12-10-05, 09:42 AM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
I was just notified about an error on my changes to the script, for which I'm sorry. Thanks to Artur Zak for noticing it.

Here's the problem:
Code:
while(this.currentBanner!=oldBanner)
..and..
 this.currentBanner=this.aNodes[Math.round((this.aNodes.length-1)*Math.random())]
should be
Code:
while(this.currentBanner==oldBanner)
..and..
this.currentBanner=Math.round((this.aNodes.length-1)*Math.random())
The first problem was random number loop. It was supposed to give you yet another random number if the first one
turned out to be the same as the old one. (The numbers are used as indexes which points to which banner should be displayed)
Instead it did the opposite and gave you a new number until it was the same as the old one. (Hence showing the same banner all the time)

The second problem was that instead of setting this.currentBanner to the random number, I used the number as the index in the array containing the banners (this.aNodes). Hence it set the variable to the actual banner object instead of the number.

These two combined to create an infinite loop which makes the browser crawl.
Since an object can never equal the number stored in oldBanner, the loop will continue forever.

Told you it wasn't tested...

To implement correct back and forward button support, I'd need to store the order in which the previus banners were shown,
you also need to allow the function to take an argument (say 1 and -1) to show which direction to step in. Then just call that function with the correct argument and you've got it.
Changing the effect is quite a bit harder since some of the banners might be flash banners...
Here's the fixed code.
Code:
// CHANGE BANNER
Banner.prototype.changeBanner = function(){
	var oldBanner=this.currentBanner
	do{
		this.currentBanner=Math.round((this.aNodes.length-1)*Math.random())
	}while(this.currentBanner==oldBanner)
	if (document.getElementById(this.aNodes[oldBanner].name).className=='m_banner_show'){
		document.getElementById(this.aNodes[oldBanner].name).className = "m_banner_hide";
	}
	document.getElementById(this.aNodes[this.currentBanner].name).className = "m_banner_show";
}
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
  #5 (permalink)  
Old 04-06-06, 04:21 PM
denrou denrou is offline
New Member
 
Join Date: Apr 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Hi, this was very helpful!

I want to bring this to a new level lol.

How can i make it so that there would be a standard file that would be loaded each time FIRST and after it's done, all other files would be randomly imported?

Thanks for your help!

Denis
Reply With Quote
  #6 (permalink)  
Old 04-06-06, 05:46 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
Here's the complete code as it is now for clarity.
I'll have to think about how to make it load a default banner before the others, I'm assuming you want the page to load a bit faster by only loading one banner when the page first loads. Then you start loading the other banners in the background and begin the rotation when it's done.

If you don't care about preloading and just want to make sure a certain banner is always displayed first, I've got a simple solution already.


I modified the str string a bit, now it only makes a few appends instead of having to do it many times.
Code:
///////////////////////////////////
// File Name: mBanner.js        //
// By: Manish Kumar Namdeo     //
// Edit: Henrik Danielsson    //
///////////////////////////////

// BANNER OBJECT
function Banner(objName){
	this.obj = objName;
	this.aNodes = [];
	this.currentBanner = 0;
};

// ADD NEW BANNER
Banner.prototype.add = function(bannerType, bannerPath, bannerDuration, height, width, hyperlink) {
	this.aNodes[this.aNodes.length] = new Node(this.obj +"_"+ this.aNodes.length, bannerType, bannerPath, bannerDuration, height, width, hyperlink);
};

// Node object
function Node(name, bannerType, bannerPath, bannerDuration, height, width, hyperlink) {
	this.name = name;
	this.bannerType = bannerType;
	this.bannerPath = bannerPath;
	this.bannerDuration = bannerDuration;
	this.height = height
	this.width = width;
	this.hyperlink= hyperlink;
//	alert (name +"|" + bannerType +"|" + bannerPath +"|" + bannerDuration +"|" + height +"|" + width + "|" + hyperlink);
};

// Outputs the banner to the page
Banner.prototype.toString = function() {
	var str = ""
	for (var iCtr=0; iCtr < this.aNodes.length; iCtr++){
		str += '<span name="'+this.aNodes[iCtr].name+'" '
		+  'id="'+this.aNodes[iCtr].name+'" '
		+  'class="m_banner_hide" '
		+  'bgcolor="#FFFCDA" '	// CHANGE BANNER COLOR HERE
		+  'align="center" '
		+  'valign="top" >\n';
		if (this.aNodes[iCtr].hyperlink != ""){
			str +=  '<a href="'+this.aNodes[iCtr].hyperlink+'">';
		}
			
		if ( this.aNodes[iCtr].bannerType == "FLASH" ){
			str +=  '<OBJECT '
			+  'classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '
			+  'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" '
			+  'WIDTH="'+this.aNodes[iCtr].width+'" '
			+  'HEIGHT="'+this.aNodes[iCtr].height+'" '
			+  'id="bnr_'+this.aNodes[iCtr].name+'" '
			+  'ALIGN="" '
			+  'VIEWASTEXT>'
			+  '<PARAM NAME=movie VALUE="'+ this.aNodes[iCtr].bannerPath + '">'
			+  '<PARAM NAME=quality VALUE=high>'
			+  '<PARAM NAME=bgcolor VALUE=#FFFCDA>'
			+  '<EMBED ';
			+  'src="'+this.aNodes[iCtr].bannerPath+'" '
			+  'quality=high '
//			+  'bgcolor=#FFFCDA '
			+  'WIDTH="'+this.aNodes[iCtr].width+'" '
			+  'HEIGHT="'+this.aNodes[iCtr].height+'" '
			+  'NAME="bnr_'+this.aNodes[iCtr].name+'" '
			+  'ALIGN="center" '
			+  'TYPE="application/x-shockwave-flash" '
			+  'PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">'
			+  '</EMBED>'
			+  '</OBJECT>'
		}else if ( this.aNodes[iCtr].bannerType == "IMAGE" ){
			str +=  '<img src="'+this.aNodes[iCtr].bannerPath+'" '
			+  'border="0" '
			+  'height="'+this.aNodes[iCtr].height+'" '
			+  'width="'+this.aNodes[iCtr].width+'">';
		}

		if (this.aNodes[iCtr].hyperlink != ""){
			str +=  '</a>';
		}

		str +=  '</span>';
	}
	return str;
};

// START THE BANNER ROTATION
Banner.prototype.start = function(){
	this.changeBanner();
	var thisBannerObj = this.obj;
	// CURRENT BANNER IS ALREADY INCREMENTED IN cahngeBanner() FUNCTION
	setTimeout(thisBannerObj+".start()", this.aNodes[this.currentBanner].bannerDuration * 5000);
}


// CHANGE BANNER
Banner.prototype.changeBanner = function(){
	var oldBanner=this.currentBanner
	do{
		this.currentBanner=Math.round((this.aNodes.length-1)*Math.random())
	}while(this.currentBanner==oldBanner)
	if document.getElementById(this.aNodes[oldBanner].name).className=='m_banner_show'){
		document.getElementById(this.aNodes[oldBanner].name).className = "m_banner_hide";
	}
	document.getElementById(this.aNodes[this.currentBanner].name).className = "m_banner_show";
}

-----------------------------------------------------------
<!--NEW BANNER AD ROTATION-->
<script language="javascript">
                    banner1 = new Banner('banner1');
                    banner1.add("FLASH", "images/ads/banners/Final_white_468x60_ver4.swf", 10, 60, 468,"http://www.actuarialcareers.com");
					banner1.add("IMAGE", "images/ads/andover2.gif", 10, 60, 468,"http://www.andoverresearch.com");
                    banner1.add("IMAGE", "images/ads/milliman_banner_revised3a.gif", 10, 60, 468,"http://www.actuarialsoftware.com");
					banner1.add("IMAGE", "images/ads/pryor_long.gif", 10, 60, 468,"http://www.ppryor.com/actuaria.htm");
                    document.write(banner1);
                    banner1.start();
</script>
<!--END NEW BANNER AD ROTATION-->
Italic is modified code. Note that any trailing semicolons must be removed when combining strings like this.
Note: Not tested after modification.
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
  #7 (permalink)  
Old 11-06-06, 06:26 PM
maya77 maya77 is offline
Newbie Coder
 
Join Date: Nov 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Hi TwoD,

I would like to make those changes (to have the banner ads appear in random order) too.

I'm using the same script but I was forced to remove <style> tags.

Instead of:
Code:
if (prevBanner >= 0){
	document.getElementById(this.aNodes[prevBanner].name).className = "m_banner_hide";
	}
	document.getElementById(this.aNodes[thisBanner].name).className = "m_banner_show";
I'm using:
Code:
if (prevBanner >= 0){
	document.getElementById(this.aNodes[prevBanner].name).style.display="none";
	}
	document.getElementById(this.aNodes[thisBanner].name).style.display="block";
Now I'm trying to do the same thing with your modified code but it doesn't work.
Instead of:
Code:
	if (document.getElementById(this.aNodes[oldBanner].name).className=='m_banner_show'){
		document.getElementById(this.aNodes[oldBanner].name).className = "m_banner_hide";
	}
	document.getElementById(this.aNodes[this.currentBanner].name).className = "m_banner_show";
I'm trying to use:
Code:
	if (document.getElementById(this.aNodes[oldBanner].name).style.display=='block'){
		document.getElementById(this.aNodes[oldBanner].name).style.display="none";
	}
	document.getElementById(this.aNodes[this.currentBanner].name).style.display="block";

Please try to help me to solve this problem. Thanks!

Last edited by maya77; 11-06-06 at 06:31 PM.
Reply With Quote
  #8 (permalink)  
Old 11-07-06, 03:52 AM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
Do you have a testpage online where the problem occurrs?
In which browsers does this occurr?

I can't see anything wrong with the modifications you've made. But try this, depending on what the page looks like it might cause problems if you set display to "block". Therefore I switched it around to check for "none" and set display to an empty string. When it's empty, a stylesheet should take over (since inlined styles have priority over other stylesheets when they are set). Since you don't have any stylesheets the default display value for that element should end up in there, making it visible.
It's just a guess tho...
Code:
if (document.getElementById(this.aNodes[oldBanner].name).style.display!='none'){
 document.getElementById(this.aNodes[oldBanner].name).style.display="none";
}
document.getElementById(this.aNodes[this.currentBanner].name).style.display="";
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
  #9 (permalink)  
Old 11-08-06, 12:40 PM
maya77 maya77 is offline
Newbie Coder
 
Join Date: Nov 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
I was complete idiot. I've changed folder name and script couldn't find any image to display. It was the only problem.

Thanks anyway TwoD!

One more question. Is it possible to change script that it loads different image on refresh but doesn't change order.

for example:

refresh1
image1 = 1.jpg
image2 = 2.jpg
image3 = 3.jpg

refresh2
image1 = 2.jpg
image2 = 3.jpg
image3 = 1.jpg

refresh3
image1 = 3.jpg
image2 = 1.jpg
image3 = 2.jpg

and so on...

3 always goes after 2 and 2 always goes after 1... etc
Reply With Quote
  #10 (permalink)  
Old 11-10-06, 01:03 AM
maya77 maya77 is offline
Newbie Coder
 
Join Date: Nov 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Sorry for bumping thread. Does anyone know how to get random start but continuous play?
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Print Ad Builder? 53effie PHP 1 07-05-05 03:24 PM
I need banner ad campaign manager software craftyweasel Script Requests 0 05-31-05 06:03 PM
Ad space on web design site: 45000 uniques - $12.50/mo magia General Advertisements 0 01-29-05 09:24 AM
PHP scripts related website - Banner Exchange? shadi Traffic Exchange 4 01-02-05 02:02 PM
Count clicks in a banner rotator mdhall PHP 9 02-29-04 02:58 PM


All times are GMT -5. The time now is 10:54 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.