Revolving Graphics (Banner Ads)

Posted on 2004-08-02
Last Modified: 2013-11-28
Can anyone help me with a very simple javascript code ... to show a random banner ad?

I have 5 images - such as the link below - each with their own very specific URL link.
I need to show one image at a time RANDOMLY.

SO if I had five images - 1,2,3,4,5 .. find a random number between 1&5 and show the appropriate image (AND it's URL) for that number.

The type of image & URL:
<a href=""><img src=""></a>

Question by:rcbuchanan
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 36

Accepted Solution

Zyloch earned 200 total points
ID: 11695436
Have two arrays. One array has the src of all the images and the other has the links for each banner repectively, meaning the first element of the image src array corresponds to the first element of the links array.

var imgSrc=new Array;
var hreflinks = new Array;

imgSrc[1] = "red.gif";
hreflinks[1] = link_for_red.html";


Then, have this code where you want the banner and link to appear inside the <body> tag:
<script language="javascript">
var randomNum = Math.floor(Math.random() * imgSrc.length);
document.write("<a href=\""+hreflinks[randomNum]+"\"><img src=\""+imgSrc[randomNum]+"\"></a>");


Assisted Solution

Xxavier earned 200 total points
ID: 11695810
var banSrc=new Array('',

var banHref = new Array('',

<a id="banLink" href="#"><img name="banImg"></a>

Ta! Da! Xx
LVL 33

Assisted Solution

humeniuk earned 100 total points
ID: 11695915
Those look like solid solutions to me.

For the hell of it, though, you can find 8 random images JavaScript scripts at and 25 random images PHP scripts at
LVL 36

Expert Comment

ID: 11696068
Are you having a problem with my script?

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Online promotion is consistently becoming more important for all types of businesses. From Facebook ads to search engines to YouTube videos, there are all sorts of channels that can effectively be used to promote a business or product. But how shoul…
Choosing a core focus or particular set of features and options can be tough. To help out, we’re going to highlight a handful of things your business needs on one of your social media pages. In other words, if one of these is missing, you should imp…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.
Suggested Courses

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question