Rotating Banners
16,098 ViewsJavascript Tutorials March 7th, 2007
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 -
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.
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

(15 votes, average: 4.2 out of 5)










September 12th, 2007 at 1:07 pm
Can you tell me how to make this only rotate through the images one time?
October 25th, 2007 at 4:41 pm
Hello Thank you so much for the script I am trying to use, I wonder if I am supposed to remove the #1 - 4 will they conflict with the code on my website that I already have? Would the script run without it, if I start with the #5 line ??? All mixed up probably this is beyond my ability to understand, though I would find it a miracle if I could and use the script. Thank you Lisa
1.
2.
3.
4. Rotating Banners
5.
6.
21.
22.
23.
24.
25.
26.
27.
28.
November 30th, 2007 at 8:51 am
Excellent forum with fantastic references and reading.... well done indeed...
http://srubibablo.com
I simply mad about this forum!
December 18th, 2007 at 2:46 am
Excellent Tutorials there.. wouldn't be more nice if you can tract it's impressions
visit my site to post ads(banner,text,image) for Free only
God Bless and more power to this site.
December 21st, 2007 at 10:35 am
Hi
Great code! Worked a treat! Is there any way to create hyperlinks (a href) from the rotating images within the code itself?
January 1st, 2008 at 3:38 am
I got it working properly, but How would I make each one of my ads clickable?
April 16th, 2008 at 8:00 am
I must really be new at this but i cannot get my pictures to show up! Please help!
June 9th, 2008 at 5:09 am
it worked well. thanks god bless u.
June 23rd, 2008 at 10:35 am
Thanks for sharing this technique!
July 8th, 2008 at 9:25 am
Wow nice little javascript ! helped me out alot ! much easier then Flash action script
October 6th, 2008 at 10:27 pm
It works like a charm but the only issue I have with it is that it doesn't validate as XHTML 1.1 because of the name attribute...
If you try validating the code at validator.w3.org, it doesn't validate because of the part that is capitalized above.
November 18th, 2008 at 5:34 am
stopping zocor abruptly http://prednisonalcohol.0catch.com/b3.html