MyBB Community Forums

Full Version: Random Banner
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How could i show random banner in my site?
Use this code where ever you want Random Banner with Link to appear

<script language="Javascript">
<!--


var currentdate = 0;
var core = 0;

function initArray() {

this.length = initArray.arguments.length;
  for (var i = 0; i < this.length; i++) {
  this[i] = initArray.arguments[i];
  }
}

link = new initArray(
"http://www.linkofsite1.com",
"http://www.linkofsite2.com",
"http://www.linkofsite3.com"

);

image = new initArray(
"http://linkofbanner1.com",
"http://linkofbanner2.com",
"http://linkofbanner3.com"

);

text = new initArray(
"Name Of Site1",
"Name Of Site2",
"Name Of Site3"

);

var currentdate = new Date();
var core = currentdate.getSeconds() % image.length;
var ranlink  = link[core];
var ranimage = image[core];
var rantext  = text[core];

document.write('<a href=\"' +ranlink+ '\" target=\"_blank\"><img src=\"'+ranimage+'\" border="0" alt=\"'+rantext+'\"></a>');

//-->
</SCRIPT>


Original article and tip by alirex: http://yaldaram.com/showthread.php?tid=490
thanks but i want show this banner on header banner only so should place above coding in index page
You've to edit header template and replace the logo URL from there. Then paste the above code in place of that.
that you placed the javascript in the header but its not working
Did you changed the Image path in the following options ?
image = new initArray(
"http://linkofbanner1.com",
"http://linkofbanner2.com",
"http://linkofbanner3.com"
Put this in your 'header' template where you would like to display the random banner:
<div id="banner">
	<a href="http://www.google.com/search?q=dragonboat">
		<img src="http://www.google.com/logos/2011/dragonboat11-hp.jpg" />
	</a>
</div>
<script type="text/javascript">
	var banners = [
		{
			'url': "http://www.google.com/search?q=dragonboat",
			'img': "http://www.google.com/logos/2011/dragonboat11-hp.jpg"
		},
		{
			'url': "http://www.google.com/search?q=scarry",
			'img': "http://www.google.com/logos/2011/scarry11-hp.png"
		},
		{
			'url': "http://www.google.com/search?q=first+man+in+space",
			'img': "http://www.google.com/logos/2011/firstmaninspace11-hp.jpg"
		}
	];
	
	var randomBanner = banners[Math.floor(Math.random()*banners.length)];
	$$('#banner a').invoke('writeAttribute','href',randomBanner.url);
	$$('#banner img').invoke('writeAttribute','src',randomBanner.img);
</script>

It uses Prototype, which is already included in MyBB.

I just grabbed some Google Doodles with the appropriate links, but you can of course change them in whatever you want and add them as long as you use the same format as I used.