Press "Enter" to skip to content

Auto-looping banners using JavaScript

Learn how to create auto looping banners using JavaScript! In this article, we learn how to rotate banners in a loop with a specified delay. The script will count the number of banners present in the specified location and set the automatic loop of all the banners.

Understanding the Source code:

There are 2 parts in this tutorial. The first part explains how to arrange the banners in a sequence and in the second one describes how the JavaScript function works.

Part1: The banners:

Each banner has to be placed inside individual “span” tags. All the banner collectively will be placed inside a single div.
By default, all the span tags [banners] will have the default “display” property as “none”. See the style sheet code for details:

div#partentDivID span{display:none;}

NOTE: In this “partentDivID” is the div tag’s Id. It is in side this div tag all the banners contain, in the order in which the banners needs to be rotated/looped. All the span tags [contains individual banner] will be in “display:none” status, initially.
Following is the HTML code in the body tag:

<div id="partentDivID">

To add more banners in the loop, just add a new span tag along with the existing span tags and place the banner inside it.
Following is the Event code for the looping function:

<script type="text/javascript">

Part2: The JavaScript Code:

In the second part of this article, let’s analyze the JavaScript function flow.

Section 1:

var bannerLoopInstance = 0;
 function rotateBanner(BannerHolder){

NOTES: “rotateBanner” funcntion is defined. At first, the function “setSpanID” is invoked and then the function “doRotation” is invoked.


function setSpanID(BannerHolderForId){
 var divInConcern = document.getElementById(BannerHolderForId);
 var spansIndivInConcern =divInConcern.getElementsByTagName("span");
 var numberOfActiveSpans = spansIndivInConcern.length;
 for(L=0;L<numberOfActiveSpans; L++) {

NOTES: The above function (setSpanID), counts the total number of span tags (banners) inside the specified div tag (partentDivID) and then sets a unique “id” value to each one of them.


function doRotation(bannerHolderDiv){
 var HolderdivInConcern = document.getElementById(bannerHolderDiv);
 var spansIndivInConcernBanner =HolderdivInConcern.getElementsByTagName("span");
 numberOfActivebanner = spansIndivInConcernBanner.length;
 bannerHolderDiv4loop =bannerHolderDiv;
 document.getElementById(bannerHolderDiv4loop+0).style.display ="block";

Notes: The above code, starts the initial looping display of banners and then after the specified time delay, a new function is invoked (rotatePlayBack).


function rotatePlayBack(maxNumberOfBannersloopBack,bannerHolderDiv4loopback){
 maxLoopVal = maxNumberOfBannersloopBack;
 currElementObj = bannerHolderDiv4loopback;

Notes: the above function sends a loop back routing to another function for mutual looping (rotatePlay), with no delay.


function rotatePlay(maxNumberOfBanners,bannerHolderDiv4loop){
 maxLoopNum = maxNumberOfBanners;
 bannerNameHolder = bannerHolderDiv4loop;
 document.getElementById(currElementObj+bannerLoopInstance).style.display ="none";
 document.getElementById(bannerHolderDiv4loop+bannerLoopInstance).style.display ="block";
 else {
 document.getElementById(bannerHolderDiv4loop+0).style.display ="block";
 setTimeout("rotatePlay(maxLoopNum,bannerNameHolder);",2000); ?
 } ?

NOTES: the above function loops through all the banners in the specified time-delay and displays them in the a loop.
Click here to download the source code: