Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2005
    Location
    auckland, new zealand
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JavaScript banner rotator not working in Firefox

    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.../TESTbanner.js
    Line: 37

    My test page can be viewed at http://www.richmastery.com/greenroom...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)

    Code:
    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
    	window.setTimeout("TTChangebannerPT()",5000);
    }
    
    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;
    		}
    	window.setTimeout("TTChangebannerPT()",5000);
    
    }
    TTLoadbannerPT();
    If I remove the ".href" and the ".src" from the following code:

    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

  • #2
    New Coder
    Join Date
    Oct 2005
    Posts
    87
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Try this:
    Code:
    document.getElementById('TestLinkPT').href = TestURLPT[TTNewbannerPT];
    document.getElementById('TestImagePT').src = TestFilePT[TTNewbannerPT];
    and I would replace the following line of code in function TTLoadbannerPT():
    Code:
    window.setTimeout("TTChangebannerPT()",5000);
    
    with this:
    
    TTChangebannerPT();
    Since you've already set a timeout in the TTChangebannerPT function, all you need to do is call that function from TTLoadbannerPT.

  • #3
    New to the CF scene
    Join Date
    Oct 2005
    Location
    auckland, new zealand
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    WOW! That is the fastest response I have ever had on a forum! :)
    Thanks pccode! Your suggestions worked great!

    Quote Originally Posted by pccode
    Try this:
    Code:
    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

  • #4
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •