Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-10-2012, 10:02 PM   PM User | #1
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
timer script not working.

Hey, I have took some example code from my javascript book which is meant to display a different image every 3 seconds. I have 2 images currently (code is setup for 3) but when the switchImage() function runs, it changes the image, but my second image is not loading, it just shows the alt attribute value and doesn't load my second image. I thought it may be an issue with the src of the images because I was using it in an external javascript file, but I moved it inline within the head of my php file and yet it still doesn't work.

Does anyone possibly know what the issue may be?

Here is the code:
Code:
var currentImgNumber = 1;
var numberOfImages = 3;

function window_onload() {
   setTimeout("switchImage()", 3000);
}

function switchImage() {
   currentImgNumber++;
   document.imgAdvert.src = 'AdvertImage' + currentImgNumber + '.jpg';
   if(currentImgNumber < numberOfImages){
      setTimeout("switchImage()", 3000);
   }
}
HTML:
Code:
<body onload="window_onload()">

<img src="images/AdvertImage1.jpg" alt="HK" name="imgAdvert" class="adverts" border="0" />
Thank you for any help,

Kind Regards,

LC.

Last edited by LearningCoder; 09-11-2012 at 08:51 PM..
LearningCoder is offline   Reply With Quote
Old 09-10-2012, 11:04 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Code:
   document.imgAdvert.src = 'AdvertImage' + currentImgNumber + '.jpg';
...
<img src="images/AdvertImage1.jpg" alt="HK" name="imgAdvert" class="adverts" border="0" />
Enough said? Apparently, you (as would be normal) don't have your images stored in two different directories. But your code says you do.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 09-10-2012, 11:13 PM   PM User | #3
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,307
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
Im too slow never mind
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Old 09-10-2012, 11:23 PM   PM User | #4
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
Quote:
Originally Posted by Old Pedant View Post
Code:
   document.imgAdvert.src = 'AdvertImage' + currentImgNumber + '.jpg';
...
<img src="images/AdvertImage1.jpg" alt="HK" name="imgAdvert" class="adverts" border="0" />
Enough said? Apparently, you (as would be normal) don't have your images stored in two different directories. But your code says you do.
I also did notice that, so I changed the line of code to read:

Code:
document.imgAdvert.src = '../images/AdvertImage' + currentImgNumber + '.jpg';
thinking that this would solve the problem but it didn't. Does anyone know what I could do to fix this? Or point me in the right direction?

Thank you for replying,

Regards,

LC.
LearningCoder is offline   Reply With Quote
Old 09-10-2012, 11:26 PM   PM User | #5
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
Ah I've got it working. It seems I didn't need to use the '../' before stating the images/ directory.

Thank you for the hint.

Regards,

LC.
LearningCoder is offline   Reply With Quote
Old 09-11-2012, 08:28 PM   PM User | #6
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
Trying to update the function at the moment. I have put the image within an anchor tag like so:
Code:
<a href="http://z3.invisionfree.com/HunterKillerz/index.php?act=idx" name="advertLink" target="_blank">
      <img src="images/AdvertImage1.jpg" alt="adverts" name="imgAdvert" class="adverts" border="0" />
   </a>
as I want the user to be able to click the image as a link. What I want my function to do is set the href attribute of the anchor tag to the relative image when it has changed.

I have updated my function but it doesn't seem to be running at all now. When I load the page, the images are not switching.

Here is my code:
Code:
var links = new Array();

links[1] = "http://z3.invisionfree.com/HunterKillerz/index.php?act=idx";
links[2] = "http://labtec.0fees.net/Gardenable/index.htm";
links[3] = "http://gamebanana.com/";

var currentImgNumber = 1;
var numberOfImages = 3;

function window_onload() {
   setInterval("switchImage()", 3000);
}

function switchImage() {
   currentImgNumber++;
   document.advertLink.href = links[currentImgNumber];
   document.imgAdvert.src = 'images/AdvertImage' + currentImgNumber + '.jpg';
   if(currentImgNumber == 3){
       currentImgNumber = 0;
   }
}
Can anyone spot any errors?

Regards,

LC.

Last edited by LearningCoder; 09-11-2012 at 08:32 PM..
LearningCoder is offline   Reply With Quote
Old 09-11-2012, 08:37 PM   PM User | #7
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 952
Thanks: 7
Thanked 98 Times in 98 Posts
WolfShade is an unknown quantity at this point
After the closing bracket, put

window.onload = window_onload;
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 09-11-2012, 08:52 PM   PM User | #8
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
Which closing bracket?

On this line:
Code:
document.advertLink.href = links[currentImgNumber];
?

Regards,

LC.
LearningCoder is offline   Reply With Quote
Old 09-11-2012, 09:14 PM   PM User | #9
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 952
Thanks: 7
Thanked 98 Times in 98 Posts
WolfShade is an unknown quantity at this point
The closing bracket after function switchImage()

Code:
function switchImage() {
    currentImgNumber++;
    document.advertLink.href = links[currentImgNumber];
    document.imgAdvert.src = 'images/AdvertImage' + currentImgNumber + '.jpg';
    if(currentImgNumber == 3){
        currentImgNumber = 0;
    }
 }
HERE
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 09-11-2012, 09:36 PM   PM User | #10
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
Ah ok, no that still doesn't work. It works if I take this line out:
Code:
document.advertLink.href = links[currentImgNumber];
What am I doing wrong? I presumed that I could just iterate through the array using the same variable as I am using to loop through the images.

I have also tried:
Code:
<a href="return switchImage()" name="advertLink" target="_blank">
   <img ......../>
</a>
Changing the function to this:
Code:
function switchImage() {
   currentImgNumber++;
   var link = document.advertLink.href = links[currentImgNumber].value;
   document.imgAdvert.src = 'images/AdvertImage' + currentImgNumber + '.jpg';
   if(currentImgNumber == 3){
       currentImgNumber = 0;
   }
   return link;
}
.....but it still didn't work...

I am getting this error in FF:[21:46:28.469] TypeError: document.advertLink is undefined @ Site/javascripts/advert.js:16

Anyone know?

Regards,

LC.

Last edited by LearningCoder; 09-11-2012 at 09:48 PM.. Reason: added more information
LearningCoder is offline   Reply With Quote
Old 09-11-2012, 09:52 PM   PM User | #11
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
Update: Just figured it out.

Changed the line to read:
Code:
document.links.advertLink.href = links[currentImgNumber];
Thanks anyway,

Kind regards,

LC.
LearningCoder is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:25 AM.


Advertisement
Log in to turn off these ads.