Javascript banner rotation

BlueDevil

New Member
Messages
5
Reaction score
0
Points
0
Javascript banner rotation

This tutorial will teach you how to create a code that makes a random image link appear each time you refresh a page. This is ideal for ad banners.

Step one

Create a file in a text editor(e.g. notepad) and save it as advert.js. This is the javascript file you will be using.
Then put in this code:
Code:
//this creates a new array called banners
banners = new Array();

//this sets strings to the values in the array
//the markup is like this: "Website url,Banner url,Site description,Banner Width,Banner Height"
banners[0] = "http://www.site.com,banner.gif,Description,468,60";
banners[1] = "http://www.site2.com,banner2.gif,Description2,200,100";
banners[2] = "http://www.site3.com,banner3.gif,Description3,700,200";

//this create a random number between 0 and the length of the array(2 in this case)
rand = Math.floor(Math.random() * banners.length);
//this splits the string in the array into the strings that were separated by commas and calls it b.
b = banners[rand].split(",");

//this creates variable for the new strings that were split from the banners array
url = b[0];
img = b[1];
des = b[2];
wid = b[3];
hei = b[4];

//this is the html markup that will display the banner on your site
document.write('<a href="' + url + '" target="_blank"><img src="' + img + '" width="'+ wid +'" height="'+ hei +'" border="0" alt="' + des + '" /></a>');

Step two

Open up your html file and put this code where you want your banner to be:
Code:
<script src="advert.js" type="text/javascript"></script>

Upload the file onto your server and you're done :)
 

tittat

Active Member
Messages
2,478
Reaction score
1
Points
38
A simple tutorial, but really helpful.
thanks BlueDevil.... :)
 
Top