PDA

View Full Version : Javascript push function


powerful_rogue
05-23-2009, 09:33 AM
Hi,

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

UKBusinessLive
05-25-2009, 07:55 PM
Hiya PR

I noticed you have a few minor errors in your code this is what the top half should look like

adImages = new Array("images/banner/dods.gif","images/banner/dovermemorial1.gif","images/banner/doverlocals.gif","images/banner/DoverLifeboatBanner.gif"");
adURLS = new Array("http://www.dods.com","http://www.dover.gov.uk","http://www.lifeboat.org.uk" );
thisAd = 0;
imgCt = adImages.length;

function rotate() {
if (document.images) {
thisAd++;
if (thisAd == imgCt) {
thisAd = 0;
}
document.adbanner.src=adImages[thisAd];
document.getElementById('ad').href = adURLS[thisAd];
setTimeout("rotate()", 14 * 1000);
}
}

Try not to use too many white spaces with Javascript, and don't leave the semi colons out neither ;)
imgCt = adImages.length;

Plus the url's will need to be the full urls complete with http://www etc....

Try changing your code to the above and see if you can get it to work, i would imagine your creating some form of banner slideshow??

Hope this helps, let us know the result