setTimeout

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eacollie
    New Member
    • Feb 2008
    • 14

    setTimeout

    Thanks for any help you can give me!
    I'm trying to set up a banner ad on a webpage that will rotate randomly through a series of about 22 ads. I found code that works for IE but I'm having problems with Firefox (it doesn't refresh unless you manually refresh the page).

    The image link for the banner that is created (by the function getCode) is:
    Code:
    <a href="http:www.website.com" target="_blank">
    <img src="Image1" width="550" height="60" border=1 onload=setTimeout('newAd()',5000);></a>
    The function newAd is as follows:
    Code:
    function newAd(){
       current_ad++;
       if (current_ad > numAds)
          current_ad = 1;
       if (document.all){
          write(getCode(current_ad));
       }
    }
  • pronerd
    Recognized Expert Contributor
    • Nov 2006
    • 392

    #2
    The IMG tag is not using proper HTML. The onload event attribute needs to be surrounded by quotes. Also you would need to check Mozilla's DOM reference to be sure, but I am not sure if they support an onLoad event for image tags.


    Code:
    onload=setTimeout('newAd()',5000);
    Should be
    Code:
    onload="setTimeout('newAd()',5000);"

    Comment

    • eacollie
      New Member
      • Feb 2008
      • 14

      #3
      Thanks pronerd.

      I changed the code to read:
      onload="setTime out('newAD(),'5 000);" but it still doesn't refresh unless you click the refresh button.

      I think Mozilla supports this:
      http://devedge-temp.mozilla.or g/library/manuals/2000/javascript/1.3/reference/handlers.html#1 120545

      Comment

      • pronerd
        Recognized Expert Contributor
        • Nov 2006
        • 392

        #4
        Oppssss. Sorry I should have seen this the first time. document.all is an IE only attribute. It is generally used as a test to check may sure you are in IE. Is there a reason you have included it in an IF test? If not try removing that if test.

        Code:
            if (document.all){
            }

        Comment

        • eacollie
          New Member
          • Feb 2008
          • 14

          #5
          Thanks pronerd.

          I'm so close!

          I made the following changes:
          Code:
          var myCode = '';
          do {
          var n= Math.floor(Math.random() * (numAds + 1) + 1);
          } while(n > numAds);
          var current_ad = n;
          myCode = getCode(n);
          
          function getCode(adNumber){
          	var tempCode = ""
          	if (ads[adNumber].href) {
          		tempCode += ('<a href="'+ ads[adNumber].href +'" target="_blank" >')
          	}
          	tempCode += ('<img src="' + ads[adNumber].src + '" width="550" height="60"  \n' )
          	tempCode += (' onLoad="setTimeout(\'newAd()\', 1000);">')
          	if (ads[adNumber].href) {
          		tempCode += ('</a>')
          	}
          	alert("The value of the variable is "+tempCode);
          	return tempCode;
          	}
          
          
          function newAd(){
          	current_ad++;
          	if (current_ad > numAds){
          		current_ad = 1;}
          	//if (document.all){
          	write(getCode(current_ad));
          	//   }
          }
          
          function write(text){
          	if (document.layers) {
          		document.bannerAd.document.write(text)
          		document.bannerAd.document.close();
          		}
          	else
          		if (document.all)
          			document.all.bannerAd.innerHTML = text	
          }
          The alert window shows the following:
          <a href="link" targer="_blank" >
          <img scr="image" width="550" height="60"
          onLoad="setTime out('newAd()';1 000);'></a>

          (I've replaced image and link, of course)

          When I load the page, I get the alert window; then shortly after I get another alert window (so it's going through the getCode() function), but it doesn't change the image and I don't get any further alerts.

          Any ideas?

          Thanks!

          Comment

          • pronerd
            Recognized Expert Contributor
            • Nov 2006
            • 392

            #6
            Ok I am still seeing a number of issues.

            1. Lines 36 and 39 are missing terminating semi-colens.

            2. The if and else's on lines 37 and 38 need to have opening and closing braces.

            3. Line 33 has an if test for support of document.layers , but then never uses it.

            4. Hard coding the DOM tree path to access elements, as is being done in the write() method is extremely error prone, and had to be tailored to each individual browser.

            5. It would be faster and easier to just change the two attributes you want to update instead of recreating all the HTML for multiple tags.


            [HTML]
            <html>
            <body>
            <a href="http://blahblahblah.co m" id="adLink">
            <img id="adImage" src="http://someAdSource.co m/" />
            </a>
            </body>
            <script>

            var adLink = document.getEle mentById('adLin k');
            var adImage = document.getEle mentById('adIma ge');

            adLink.setAttri bute('href', 'http://whatEverNewLink .com');
            adImage .setAttribute(' src', 'http://someNewImageLin k.com');

            </script>
            </html>
            [/HTML]

            Comment

            Working...