Results 1 to 9 of 9

Thread: Help with javascript push function

  1. #1
    Join Date
    May 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with javascript push function

    Hi,

    Im new to javascript and have just been playing with the following code, but for some reason cant seem to get the "push" function to operate.

    This is my code below

    PHP Code:
    <title>Image Array</title>

    <
    SCRIPT LANGUAGE=JAVASCRIPT TYPE="TEXT/JAVASCRIPT">

    adImages = new Array("images/banner/dods.gif","images/banner/dovermemorial1.gif","images/banner/doverlocals.gif","images/banner/DoverLifeboatBanner.gif")
    adURL = new Array("dods.com""dover.gov.uk","doverlocals.co.uk","lifeboat.org.uk")

    thisAd 0
    imgCt 
    adImages.length

      
    function rotate() {
      if (
    document.images) {
      
    thisAd++
      if (
    thisAd == imgCt) {
      
    thisAd 0
      
    }
      
    document.adBanner.src=adImages[thisAd]
      
    setTimeout("rotate()" imgCt 1000)
      }
      
      }
      
    function 
    newLocation() {
     
    document.location.href "http://www." adURL[thisAd]
     }
      
    adImages.push("images/banner/freecycle.jpg")
      
    adURL.push("freecycle.org")

      
    </script>
      
    </head>

    <BODY BGCOLOUR=BLUE onLoad="rotate()">
    <center>
    <A HREF="javascript:newLocation()">
    <IMG SRC="images/banner/dods.gif" WIDTH=468 HEIGHT=60 NAME="adBanner" BORDER="0"></A>

    </center>
    </body>
    </html> 
    I want to be able to add a banner onto the end of adImages and a webaddress onto the end of adURL

    The current arrays work fine, however I cant seem to get the new ones in the push command to be added!

    Any help greatly appreciated!

  2. #2
    Join Date
    Jul 2008
    Posts
    128
    Thanks
    0
    Thanked 17 Times in 16 Posts

    Default

    Quote Originally Posted by powerful_rogue View Post
    Hi,

    Im new to javascript and have just been playing with the following code, but for some reason cant seem to get the "push" function to operate.
    imgCt is never updated.

  3. #3
    Join Date
    May 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ah,

    I thought because imgCt=adImages.length , when a new item was added into the array it would update the length.

    Is there anyway around this at all?

  4. #4
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    Why do you add the extra images this way, why not just add them to the array at the top. This way imgCT will be accurate.

    To see if your push function is working put this in your code

    print_r(adImages);

    print_r(adURL);
    Last edited by forum_amnesiac; 05-25-2009 at 02:40 PM.

  5. #5
    Join Date
    May 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Im trying to create a mod for vbulletin using javascript.

    An admin can add images via a form and they get added into the array so they show on the forum.

    Im guessing im going the wrong way about this

  6. #6
    Join Date
    Jul 2008
    Posts
    128
    Thanks
    0
    Thanked 17 Times in 16 Posts

    Default

    Quote Originally Posted by powerful_rogue View Post
    Ah,

    I thought because imgCt=adImages.length , when a new item was added into the array it would update the length.

    Is there anyway around this at all?

    Just read adImages.length directly.

  7. #7
    Join Date
    May 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by clueful View Post
    Just read adImages.length directly.
    Sorry, Im still learning!

    How do you mean?

  8. #8
    Join Date
    May 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by forum_amnesiac View Post
    Why do you add the extra images this way, why not just add them to the array at the top. This way imgCT will be accurate.

    To see if your push function is working put this in your code

    print_r(adImages);

    print_r(adURL);
    I put those two lines into the code, at various places but no luck.

    Is my push function in the right place?

  9. #9
    Join Date
    Jul 2008
    Posts
    128
    Thanks
    0
    Thanked 17 Times in 16 Posts

    Default

    Quote Originally Posted by powerful_rogue View Post
    Sorry, Im still learning!

    How do you mean?
    Something like this, although It's not clear what should determine the delay period:
    Code:
    function rotate() 
    {
      if (document.images) 
      {
       if ( ++thisAd == adImages.length ) 
        thisAd = 0;
      
       document.images.adBanner.src = adImages[ thisAd ] ;
    
       setTimeout( rotate , imgCt * 1000 );
      }
    }

Bookmarks

Posting Permissions

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