473,320 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. <a href="http:www.website.com" target="_blank">
  2. <img src="Image1" width="550" height="60" border=1 onload=setTimeout('newAd()',5000);></a>
The function newAd is as follows:
Expand|Select|Wrap|Line Numbers
  1. function newAd(){
  2.    current_ad++;
  3.    if (current_ad > numAds)
  4.       current_ad = 1;
  5.    if (document.all){
  6.       write(getCode(current_ad));
  7.    }
  8. }
Mar 31 '08 #1
5 1027
pronerd
392 Expert 256MB
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.


Expand|Select|Wrap|Line Numbers
  1. onload=setTimeout('newAd()',5000);
  2.  
Should be
Expand|Select|Wrap|Line Numbers
  1. onload="setTimeout('newAd()',5000);"
  2.  
Mar 31 '08 #2
Thanks pronerd.

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

I think Mozilla supports this:
http://devedge-temp.mozilla.org/library/manuals/2000/javascript/1.3/reference/handlers.html#1120545
Mar 31 '08 #3
pronerd
392 Expert 256MB
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.

Expand|Select|Wrap|Line Numbers
  1.     if (document.all){
  2.     }
  3.  
Apr 1 '08 #4
Thanks pronerd.

I'm so close!

I made the following changes:
Expand|Select|Wrap|Line Numbers
  1. var myCode = '';
  2. do {
  3. var n= Math.floor(Math.random() * (numAds + 1) + 1);
  4. } while(n > numAds);
  5. var current_ad = n;
  6. myCode = getCode(n);
  7.  
  8. function getCode(adNumber){
  9.     var tempCode = ""
  10.     if (ads[adNumber].href) {
  11.         tempCode += ('<a href="'+ ads[adNumber].href +'" target="_blank" >')
  12.     }
  13.     tempCode += ('<img src="' + ads[adNumber].src + '" width="550" height="60"  \n' )
  14.     tempCode += (' onLoad="setTimeout(\'newAd()\', 1000);">')
  15.     if (ads[adNumber].href) {
  16.         tempCode += ('</a>')
  17.     }
  18.     alert("The value of the variable is "+tempCode);
  19.     return tempCode;
  20.     }
  21.  
  22.  
  23. function newAd(){
  24.     current_ad++;
  25.     if (current_ad > numAds){
  26.         current_ad = 1;}
  27.     //if (document.all){
  28.     write(getCode(current_ad));
  29.     //   }
  30. }
  31.  
  32. function write(text){
  33.     if (document.layers) {
  34.         document.bannerAd.document.write(text)
  35.         document.bannerAd.document.close();
  36.         }
  37.     else
  38.         if (document.all)
  39.             document.all.bannerAd.innerHTML = text    
  40. }
The alert window shows the following:
<a href="link" targer="_blank">
<img scr="image" width="550" height="60"
onLoad="setTimeout('newAd()';1000);'></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!
Apr 1 '08 #5
pronerd
392 Expert 256MB
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.com" id="adLink">
<img id="adImage" src="http://someAdSource.com/" />
</a>
</body>
<script>

var adLink = document.getElementById('adLink');
var adImage = document.getElementById('adImage');

adLink.setAttribute('href', 'http://whatEverNewLink.com');
adImage .setAttribute('src', 'http://someNewImageLink.com');

</script>
</html>
[/HTML]
Apr 1 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Jupiter49 | last post by:
I'm trying to move pictures around on the screen, with a slight delay between the first picture and second picture (following onmousemove). When I add the setTimeout function I must be doing it...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
2
by: Athanasius | last post by:
Could someone shed some light as to why the following setTimeout function will not work on the Mac IE5.2? It does however work on PC(Forefox,Netscape,IE) & Mac(Safari,Firefox). Here is the script,...
6
by: Brent | last post by:
Is there any obvious reason why there's no delay in the execution of setting the id.style.display when this function gets called? function Hide(divId,timeout) { var id =...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
28
by: Andre | last post by:
Hi, Does anyone know whether the ECMA, or an other standard document, specifies a maximum for the value that can be pass to the setTimeOut() function in Javascript? Andre
4
by: E | last post by:
I am having trouble with setTimeout working on a second call to the setTimeout function from a second page which is an html page. Here is the scenario. I have a web page and onload it calls a...
6
by: Max | last post by:
Hi, I'm not exactly sure why this doesn't work. I'm basically just trying a simple approach at a slide down div. function slide_div { ...
15
by: nikki_herring | last post by:
I am using setTimeout( ) to continuously call a function that randomly rotates/displays 2 images on a page. The part I need help with is the second image should rotate 3 seconds after the first...
7
by: Martin | last post by:
Can I have two setTimeouts running at the same time - with two different intervals? I want to start one timer and, before it times out, start another one I've tried this and they seems to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.