Tuesday, April 23, 2013

Rotating Banner for your website with HTML and JavaScript

What is Rotating Banner?

Web banners play an immense part in today's advertising methods. While browsing in the web, you can see advertising banners that is cycling through a number of images each of them linking to specific target pages. Let us see the code to create such a rotating web banner to be used in your website and bit of understanding about the code.

I am using Dreamweaver as my HTML editor software. You can use any available editors of your choice.
  • Create a new HTML file and my editor automatically provides the very basic tags embedded within the file.
  • Create a top section on your web page for the banner to be displayed using <div> tag 
<div>
  <a href="_blank"><img src ="images/Chrysanthemum.jpg" height="200" width="1345"  id="banner"/>
   </a>
</div>

My first image on the rotating banner is "Chrysanthemum.jpg" placed in images folder with id attribute for <img> tag as "banner".
  •  Now enter into the JavaScript code within the script tags.
<script language="javascript" type="text/javascript">
</script>
  • We write a rotate() function to flip through the images with a tiny time gap. Create a variable "ad" and initialise it to "0" outside the function.
function rotate()
 {
     var images = new Array ("images/Chrysanthemum.jpg", "images/Desert.jpg", "images/Hydrangeas.jpg");
     ad++;
     if (ad == images.length)
      {
        ad = 0; 
      }
     document.getElementById("banner").src = images [ad] ;
     setTimeout(rotate, 2000);
 }


Within the function a new array called images is created with the image files. The setTimeout function is used to call the rotate() function every 2000 milliseconds. Each time the function is called the "ad" value will be incremented and the "src" attribute of the banner element will be substituted with image from the images array with index as the current "ad" value.
  • We need to add links to each image with a function named newlocation().
function newlocation ()
 {
     var url = new Array ("http://www.google.com","http://www.yahoo.com","http://www.bing.com");
     document.location.href = url [ad];
     return false;
 }


Within this function a new array called url is created with the links to each image. The entire URL of the document is substituted with link from the url array with index as the current ad value.
  • Its time to call the functions now as below.
 document.getElementById("banner").parentNode.onclick = newlocation;
 rotate();


Whenever an image is clicked the newlocation() function is called with the help of onclick event and rotate() function is called directly. This is the basic code for rotating banner and you can add any style to your banner with the help of CSS.

Download code here

10 comments:

  1. I am playing around with your code. It seems the 1st banner image you set in the "div" doesnt show up when you first load the page, however it show up when the banner rotation gets to the last image and loops back to the 1st

    ReplyDelete
    Replies
    1. rotate();
      ===>
      window.onload=rotate;

      Delete
  2. Replies
    1. Hi Jeff,

      I have modified the code slightly so as to display the images in correct order and attached the reformed file.

      Delete
  3. Hi Jeff,

    I will modify the code and post in a couple of days. Thanks for the notification.

    ReplyDelete
  4. This script is great!
    Thank you!
    Ihave one question...
    I am trying to use this script in a couple of locations on the same page.
    In attempting to duplicate this script/div, only the first instance will cycle through ads. the others will not function.
    I don'tknow a lot about JS, but is there a way around this?

    Thanks!

    ReplyDelete
    Replies
    1. Hi 10spd,

      I have done few changes in the script in order to make the images to roatate in a couple of locations. You can download the modified script in the below link.

      https://sites.google.com/site/httptechzioblogspotcom/rotatebanner1.txt?attredirects=0&d=1

      Thanks!

      Delete
    2. Hi sp.techzio, my first image at load is still coming up broken, any suggestions?

      Delete
    3. Hi,

      The script attached should be working fine. It does for me. I can provide suggestions, if I could see the exact script you are using and a better description about the problem. You can even email your queries to sp.techzio@gmail.com.

      Delete
  5. hey thank you this is working but i cant find image when i open my web page first time

    ReplyDelete