View Full Version : JavaScript banner rotator not working in Firefox

10-18-2005, 01:11 AM
Hi there,

I have recently started a new job, and this code was written by someone I can't get hold of anymore. I have tried to find a reason for the following script not working in Firefox, but I can't seem to find one anywhere.

The script is supposed to load an image and a link using an array, and then change it after the timeout. This seems to work fine in IE, but in Firefox it does nothing, and even worse returns an error if you check the Firefox JavaScript Console:

Error: TestLinkPT is not defined
Source File: http://www.richmastery.com/greenroom/bannerTEST/TESTbanner.js
Line: 37

My test page can be viewed at http://www.richmastery.com/greenroom/bannerTEST/bannerTEST.htm

Here is the source of the .js file: (sorry for posting the whole lot, but I don't know if I've maybe missed something obvious)

var TestURLPT = new Array(4);
TestURLPT[0] = "http://www.test.com/page0.html";
TestURLPT[1] = "http://www.test.com/page1.html";
TestURLPT[2] = "http://www.test.com/page2.html";
TestURLPT[3] = "http://www.test.com/page3.html";

var TestFilePT = new Array(4);
TestFilePT[0] = "jpeg0.jpg";
TestFilePT[1] = "jpeg1.jpg";
TestFilePT[2] = "jpeg2.jpg";
TestFilePT[3] = "jpeg3.jpg";

var TTNewbannerPT = 3;
var TTCurrentbannerPT = 3;
// Start with random banner
//var TTNewbannerPT = Math.floor(Math.random() * TestFilePT.length);
//var TTCurrentbannerPT = Math.floor(Math.random() * TestFilePT.length);

function TTLoadbannerPT(){
document.write("<A NAME='TestLinkPT' ID='TestLinkPT' HREF='" + TestURLPT[TTNewbannerPT] + "' target='_blank'>");
document.write("<IMG NAME='TestImagePT' ID='TestImagePT' SRC='" + TestFilePT[TTNewbannerPT] + "' width='200' height='200' border='0' style='border: 1px solid #222222;'></A>");

//Set the current banner to a new number
TTCurrentbannerPT = TTNewbannerPT;

//Set the Timeout

function TTChangebannerPT(){
//Get a new banner
TTNewbannerPT = Math.floor(Math.random() * TestFilePT.length);

//If is not the current banner change the SRC and URL, then assign the new banner
if(TTCurrentbannerPT != TTNewbannerPT){
TestLinkPT.href = TestURLPT[TTNewbannerPT];
TestImagePT.src = TestFilePT[TTNewbannerPT];
TTCurrentbannerPT = TTNewbannerPT;


If I remove the ".href" and the ".src" from the following code:

TestLinkPT.href = TestURLPT[TTNewbannerPT];
TestImagePT.src = TestFilePT[TTNewbannerPT];

It no longer returns an error in Firefox, but it does in IE?

Thank you very much to anyone who can take the time out to have a look at this, I'm fairly new to JavaScript, and any help would be much appreciated :)

- Charlie

10-18-2005, 01:29 AM
Try this:

document.getElementById('TestLinkPT').href = TestURLPT[TTNewbannerPT];
document.getElementById('TestImagePT').src = TestFilePT[TTNewbannerPT];

and I would replace the following line of code in function TTLoadbannerPT():


with this:


Since you've already set a timeout in the TTChangebannerPT function, all you need to do is call that function from TTLoadbannerPT.

10-18-2005, 01:56 AM
WOW! That is the fastest response I have ever had on a forum! :)
Thanks pccode! Your suggestions worked great!

Try this:

document.getElementById('TestLinkPT').href = TestURLPT[TTNewbannerPT];
document.getElementById('TestImagePT').src = TestFilePT[TTNewbannerPT];

This part seems to have fixed my problem, and the banner works on both IE and Firefox now!

Thanks again for your awesome help!

- Charlie

04-01-2008, 10:57 PM
I've been looking for some code that does just this and works in Firefox as well as IE. I just need one change: some of my banner images don't have a corresponding website, so I want to generate just the <img> tag without the <a href> tag. Can someone show me how to do this?

Thanks much!