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 11 of 11
  1. #1
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    912
    Thanks
    76
    Thanked 28 Times in 28 Posts

    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.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.

  • #3
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    Im too slow never mind

  • #4
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    912
    Thanks
    76
    Thanked 28 Times in 28 Posts
    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.

  • #5
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    912
    Thanks
    76
    Thanked 28 Times in 28 Posts
    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.

  • #6
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    912
    Thanks
    76
    Thanked 28 Times in 28 Posts
    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.

  • #7
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    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".

  • #8
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    912
    Thanks
    76
    Thanked 28 Times in 28 Posts
    Which closing bracket?

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

    Regards,

    LC.

  • #9
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    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".

  • #10
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    912
    Thanks
    76
    Thanked 28 Times in 28 Posts
    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

  • #11
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    912
    Thanks
    76
    Thanked 28 Times in 28 Posts
    Update: Just figured it out.

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

    Kind regards,

    LC.


  •  

    Posting Permissions

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