Rotating Banners
66,343 viewsJavascript Tutorials March 7th, 2007
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

(25 votes, average: 3.96 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.
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.
December 16th, 2008 at 3:53 am
My site header wont rotate…please look and the script and advise….thanks!!
7 Co-op Image Group :: Graphic and Website Design Company
adImages = new Array(“images/7CIG_TOP_BCs.jpg”,”images/7CIG_Flyers.jpg”,”images/7CIG_logos.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)
}
}
1
December 16th, 2008 at 3:54 am
7 Co-op Image Group :: Graphic and Website Design Company
adImages = new Array(“images/7CIG_TOP_BCs.jpg”,”images/7CIG_Flyers.jpg”,”images/7CIG_logos.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)
}
}
1
December 16th, 2008 at 3:55 am
Sorry guys the script is too long
October 23rd, 2010 at 6:10 pm
Most a fascinating post and reader comments that you have here. I would like to point out that others may make a varying angle, especially in regards to natural health. Has anyone here located good viewpoints on the Internet, and will you let me know where?
May 20th, 2011 at 7:44 am
Hi, very useful, what about if I want to rotate text instead?
June 7th, 2011 at 12:36 pm
Thanks for this tutorial! 2 Qs. 1) How much time do you think it takes to add the rotating banner code? And 2) can you set the rotation to every 15 minutes? I’d like my three banners to have four ads each and rotate together every 15 minutes. Thanks again!
December 21st, 2012 at 12:09 pm
Does anyone know how to make it cycle through the rotation once (only 1 time)? I see the question referenced above but the solution is not shown. Would love to know if there is a solution … thanks !!