advertisement

Learn how to create a banner system that rotates your banners without the user having to refresh. Using javascript you can make a nicely flowing rotating banners script that doesn’t require the user to refresh at anytime, good for ad systems.

Alright, let’s jump right into this tutorial.

First we start off our HTML document -

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Rotating Banners</title>
<script language="Javascript" type="text/javascript">
<!-- Hide from old browsers

Alright so all this piece of code does is start our HTML document and then start a Javascript area to put some javascript code.

Now we need to create the actual code that goes into the javascript area.

adImages = new Array("images/banner.jpg","images/banner1.jpg","images/banner2.jpg")
thisAd = 0
imgCt = adImages.length

This piece of code creates 3 new variables, our first variable adImages is a varialbe that an array is set to wtih the URL’s to our banners. Then our other 2 variables are used for the length of our banner rotation and how many banners we have to rotate.

function rotate() {
if (document.images) {
thisAd++
if (thisAd == imgCt) {
thisAd = 0
}

That last piece of code creates a new function called rotate(). This is the function that actually rotates our banners, thisAd++ is what adds one to our counter and rotates our banner.

document.adBanner.src=adImages[thisAd]
setTimeout("rotate()", 3 * 1000)
}
}
// End hide script from old browsers -->
</script>
</head>

This piece of code sets the timeout for our banner, in other words how long it takes for the banners to rotate in between each banner.

<body onload="rotate()">
<center>
<img src="images/banner1.gif" name="adBanner" alt="Ad Banner" />
</center>
</body>
</html>

This last piece of code finishes off our script. Once the document is loaded our function rotate() is loaded. (This is what the onload=”rotate()” does in our body tag). Then we put in our image and give it a name of “adBanner”. This corresponds to what we put in our javascript function, so now it knows which image to rotate.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Rotating Banners</title>
<script language="Javascript" type="text/javascript">
<!-- Hide from old browsers
adImages = new Array("images/banner.jpg","images/banner1.jpg","images/banner2.jpg")
thisAd = 0
imgCt = adImages.length
function rotate() {
if (document.images) {
thisAd++
if (thisAd == imgCt) {
thisAd = 0
}
document.adBanner.src=adImages[thisAd]
setTimeout("rotate()", 3 * 1000)
}
}
// End hide script from old browsers -->
</script>
</head>
<body onload="rotate()">
<center>
<img src="images/banner1.gif" name="adBanner" alt="Ad Banner" />
</center>
</body>
</html>

Theres our finished code.

Now we just need 3 images and place them all in an images directory.

I have been nice and created 3 banners, you can use them on your website. I actually encourage you use it (Well the one that is nicest)

http://www.tutorialcode.com/images/banner.jpg

http://www.tutorialcode.com/images/banner1.jpg

http://www.tutorialcode.com/images/banner2.jpg

So as you can see I have created 3 banners, use them if you want.

So, that’s all! Thanks for reading my tutorial. If you have any questions use the contact form and we will address your comments or questions!

Sean

advertisement