PDA

View Full Version : Help with javascript push function



powerful_rogue
05-24-2009, 03:42 PM
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


<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!

clueful
05-25-2009, 07:23 AM
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.

powerful_rogue
05-25-2009, 11:20 AM
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?

forum_amnesiac
05-25-2009, 02:34 PM
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);

powerful_rogue
05-25-2009, 03:56 PM
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 :(

clueful
05-25-2009, 05:03 PM
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.

powerful_rogue
05-25-2009, 06:21 PM
Just read adImages.length directly.

Sorry, Im still learning!

How do you mean?

powerful_rogue
05-25-2009, 06:22 PM
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?

clueful
05-25-2009, 11:14 PM
Sorry, Im still learning!

How do you mean?

Something like this, although It's not clear what should determine the delay period:
function rotate()
{
if (document.images)
{
if ( ++thisAd == adImages.length )
thisAd = 0;

document.images.adBanner.src = adImages[ thisAd ] ;

setTimeout( rotate , imgCt * 1000 );
}
}