Transact SQL Other Articles Software Reviews
This script was written to address some of the shortcomings of some other methods for creating rotating banner advertisements "Banner Ads", namely:
The script described here addresses both problems. It displays a different image after a specific amount of time, and clicking on each of the images in the sequence will take you to a different URL. An example banner can be viewed below:
Adding the script to a page
There are several requirements for this script:
You need several, identically sized images. The images can be in any format supported by browsers (i.e. GIF, PNG or JPEG). The example described in this article uses four different images, each of which is 468 pixels wide and 60 pixels high.
You also need to modify the <BODY> HTML tag of the page so that when the page is loaded, it executes the InitialiseBannerAdRotator function. Do this by adding the onload tag, as shown below:
Finally, you need to add the following script to the page (within the <HEAD> part of the page is the ideal location):
A link at the bottom of this article will allow you to download a ZIP file containing the script and example images.
The script has a number of parameters in the section headed "User defined variables", which you will need to change in order to customise the script. The parameters are described below:
How the Script Works
When the web page is first loaded into the web browser, it runs the function InitialiseBannerAdRotator. The first part of this function determines the name and version of the browser the script is being run on, setting the variable IsValidBrowser to true if the browser is supported by the script. Browsers supported by the script are Netscape versions of 3 and above, and Microsoft Internet Explorer 4 and above.
The InitialiseBannerAdRotator function then sets a timer called "TimerObject" for the time interval specified in the user defined variables. When this timer reaches zero, it then runs the ChangeImage function (the use of which is described further on).
The function also assigns zero to the BannerAdCode variable. This variable is used to cycle through the different images to be shown. Finally, the function pre-loads the images required by the script.
The second function in the script is called ChangeImage. This function increases the value of the BannerAdCode variable, ensuring that if it is too large for the number of different images, it is reset to zero. After changing the BannerAdCode variable, the function changes the image source ("src") of the image that has the name of "bannerad" to point to the next image in the sequence. This is the part of the script which actually changes the image on the page.
There can only be one instance of a banner ad per page on your site, although with a few modifications the script could support several banners per page. Don't forget that adding too much advertising to your site will distract users, so use sparingly.
The script currently cycles through a series of images, but it would not be too difficult to modify it to show the images in a random order.
The script doesn't open links in a new browser window. If you want the commercial version of the script that does, then you can purchase the full version for just $3.
If you purchase any of my other software products (ASP Documentation Tool, .NET Documentation Tool, The Website Utility, Indexing Service Companion) then let me know and I'll send you the script for free!
If you would like the Banner Ad Rotator script together with some sample images, then click on the link below: