473,839 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

setTimeout

14 New Member
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 1038
pronerd
392 Recognized Expert Contributor
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
eacollie
14 New Member
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
Mar 31 '08 #3
pronerd
392 Recognized Expert Contributor
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
eacollie
14 New Member
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="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!
Apr 1 '08 #5
pronerd
392 Recognized Expert Contributor
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]
Apr 1 '08 #6

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

Similar topics

10
3079
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 wrong because the function that calculates the new coordinates for the picture returns 32 million and change instead of the coordinate. This causes the picture to not be able to appear on the display, obviously. Here's the calling code (snipped...
3
14968
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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
2
4682
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, it should simply present an alert message after 5 seconds. Thanks. <html> <head> <script language="javascript"> function MsgTimer() { var self = this;
6
2257
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 = document.getElementById(divId); setTimeout(id.style.display = 'none',timeout); } Thanks for any help.
12
5563
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. Foo = function(type) { this.num = 0; this.type = type this.trigger(); } Foo.prototype.trigger = function() {
28
5316
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
5245
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 javascript function which calls setTimeout and will process a second javascript function "Warn" just before the session expires. The Warn function displays an html page with a button. A second timer is started to cause the html page to close...
6
10925
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 { setTimeout("document.getElementById('mydiv').style.length='10px';",1000);
15
3804
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 image rotates. I cannot figure out how to accomplish the 3 second delay. My code is pasted below: function randPic(){ randPic1(); randPic2();
7
2419
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 interfer with one another.
0
9697
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10909
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10650
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9426
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7830
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7019
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4492
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4065
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.