Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Banner Null object problem...ongoing...help please!! :(

Status
Not open for further replies.

flipside1212

Technical User
Jun 24, 2003
4
CA
Hi. I'm having a problem with my random ad banner. It keeps giving the error "BannerAdImages[..].src is null or not an object" It happens a minute or two after each page loads and on different images. Here is the code:

<SCRIPT LANGUAGE=&quot;javascript&quot;><!--

var ImageFolder = &quot; //Folder name containing the images
var ImageFileNames = new Array('ad1_beatstreet_small.jpg', 'ad2_zionsgate_small.jpg', 'ad3_brina_small.jpg', 'ad4_metro3106_small.jpg', 'ad5_eightzero_small.jpg', 'ad6_dadabase_small.jpg', 'ad7_karmajeans_small.jpg', 'ad9_blueskies_small.jpg', 'ad10_bcalhoun_small.jpg');
var ImageURLs = new Array(' ' ' ' ' ' ' ' 'var DefaultURL = 'var DisplayInterval = 7;
var TargetFrame = &quot;&quot;;
var IsValidBrowser = false;
var BannerAdCode = 0;
var BannerAdImages = new Array(NumberOfImages);
var DisplayInterval = DisplayInterval * 1000;
var NumberOfImages = ImageFileNames.length;

if (ImageFolder.substr(ImageFolder.length - 1, ImageFolder.length) != &quot;/&quot; && ImageFolder != &quot;&quot;) { ImageFolder += &quot;/&quot;;
}

if (TargetFrame == '') {
var FramesObject = null;
} else {
var FramesObject = eval('parent.' + TargetFrame);
}

function InitialiseBannerAdRotator() {

var BrowserType = navigator.appName;
var BrowserVersion = parseInt(navigator.appVersion);

if (BrowserType == &quot;Netscape&quot; && (BrowserVersion >= 3)) {
IsValidBrowser = true;
}

if (BrowserType == &quot;Microsoft Internet Explorer&quot; && (BrowserVersion >= 4)) {
IsValidBrowser = true;
}

if (IsValidBrowser) {
TimerObject = setTimeout(&quot;ChangeImage()&quot;, DisplayInterval);
BannerAdCode = 0;

for (i = 0; i < NumberOfImages; i++) {
BannerAdImages = new Image();
BannerAdImages.src = ' ' + ImageFolder + ImageFileNames;
}
}
}

function ChangeImage() {

BannerAdCode = (Math.round((Math.random()*8)+1));
window.document.bannerad.src = BannerAdImages[BannerAdCode].src;
TimerObject = setTimeout(&quot;ChangeImage()&quot;, DisplayInterval);
}

function ChangePage() {

if (IsValidBrowser) {window.open(ImageURLs[BannerAdCode]);}
else if (!IsValidBrowser) {document.location = DefaultURL;}

}

// --></script>

<A HREF=&quot;javascript:ChangePage()&quot;><img src=&quot;ads/ad1_beatstreet_small.jpg&quot; alt=&quot;Banner Advertisement&quot; border=&quot;0&quot; hspace=&quot;0&quot; name=&quot;bannerad&quot; WIDTH=&quot;230&quot; HEIGHT=&quot;85&quot;></a>

************Your help is greatly appreciated!!!!
 
[tt]BannerAdCode = (Math.round((Math.random()*8)+1));[/tt]

Will assign a number from one to eight (inclusive) to the [tt]BannerAdCode[/tt] variable.

However, arrays in Javascript start at index 0. Whenever [tt]BannerAdCode[/tt] gets assigned a '7' or '8' your code looks for the image URL at the eighth or ninth position in the array (which are both blank), hence the error.

Change the random number generator to:
[tt]BannerAdCode = (Math.round(Math.random()*7));[/tt]

Which will return a number between zero and six (inclusive), more closely matching the range of index values in your array.
 
Eh, sorry, didn't count the full number of elements in your array...

The other problem is that you set the length of the BannerAdImages to NumberOfImages before you assign a value to the NumberOfImages variable as so:
[tt]
var BannerAdImages = new Array(NumberOfImages);
var DisplayInterval = DisplayInterval * 1000;
var NumberOfImages = ImageFileNames.length;
[/tt]

This is not a huge problem as you add the elements to the array later.

However, as I stated (incorectly) before [tt]BannerAdCode = (Math.round((Math.random()*8)+1));[/tt] will return either 1, 2, 3, 4, 5, 6, 7, 8, or 9.

It will be that 9 causing the problem - 9 referring to the empty tenth spot in the array.

May I suggest an even better approach:
[tt]BannerAdCode = (Math.floor(Math.random()*NumberOfImages));[/tt]

Which will return anything from zero up to however many images are in the array.
 
thank you so much, you are a genius. Saved me hours of trial and error bullcrap. Thanks once again!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top