Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Undefined Array Problem

 
Blaise Harriman
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm new to javascript and have a small but annoying problem with an array. The script rotates a randomly selected banner from an array and changes the banner every few seconds. The problem is that when the page is first loaded the word 'undefined' displays next to the first banner. When the second banner is displayed everything works fine.

Here is the function I am using

function randomBanner()
{

var myBanners=new Array(3);
var i = Math.floor(Math.random() * 3);


myBanners[0]="image 1";
myBanners[1]="image 2";
myBanners[2]="image 3";


document.getElementById('banners').innerHTML = myBanners[i];

setTimeout("randomBanner()",5000 );//5000 = 5 seconds

}

and here is the html code

<head>
<script type="text/javascript" src="rotator.js"></script>
</head>

<body>
<div id="banners">
<script type="text/javascript">document.write(randomBanner());</script>
</div>
</body>


I have tried declaring the variables and even the array itself outside the function to no avail. I have set up a test page so you can view the problem here

Please can someone help?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi and welcome to the JavaRanch.

Keep in mind that Java != Javascript. They are 2 different languages. I've moved your topic to a more suitable forum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic