Forum Moderators: open

Message Too Old, No Replies

Banner Rotator Problem

Javascript Banner Rotator

         

tcustom

8:36 pm on Dec 26, 2003 (gmt 0)

10+ Year Member



New here .....and to Javascript.

Inherited a .js file below that rotates banners, and users are able to click on a link to open the sponsor link in a new window.

I believe it was supposed to work in Netscape an IE, but of course, the banners do not rotate in Netscape. Only the first image banner displays?

banner.js file:

var imgs1 = new Array("image1.jpg","image2.gif","etc...")
var lnks1 = new Array("link1","link2", "etc....")
var alt1 = new Array("image1text","image2text","etc..")
var currentAd1 = 0
var imgCt1 = 3
function cycle1() {
currentAd1++
if (currentAd1 == imgCt1) {
currentAd1 = 0
}
document.adBanner1.src=imgs1[currentAd1]
document.adBanner1.alt=alt1[currentAd1]
adLink1.href=lnks1[currentAd1]
setTimeout("cycle1()",10 * 1000)
}

For the imgs1, I am using external image files, as with the links in the lnks1 variable. The imgCt1 is how many images you choose to include and should match the number of images in imgs1. Works great in IE....

This code is place in the appropriate spot within the HTML doc:

<script type="text/javascript" language="JavaScript" src="banner.js">
</script>
<a href="any link" name="adLink1" target="_blank"><img alt="" src="whatever image" name="adBanner1" border="0" width="468" height="60"></a>
<script type="text/javascript" language="JavaScript">
cycle1()
</script>

Sure it could be an easy answer, but I just don't have the experience w/ javascript and how it behaves in different browsers.

Thanks.....

tedster

2:13 am on Dec 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't immediately see the problem. Have you tried opening the Javascript Console in Netscape to get more information? In NN7 you find it under

Tools > Web Development > Javascript Console

tcustom

3:32 pm on Dec 28, 2003 (gmt 0)

10+ Year Member



ok, it says:

Error: adLink1 is not defined

and references this line in the code above:

adLink1.href=lnks1[currentAd1]

is this defined okay for Netscape?

tcustom

4:26 pm on Dec 30, 2003 (gmt 0)

10+ Year Member



Can anyone else see a problem here?

webkid

5:11 pm on Dec 30, 2003 (gmt 0)

10+ Year Member



mm..

Hi there..

Here is the free example of the banner rotator, you may take a look...

[javascript.internet.com...]

garann

6:36 pm on Dec 30, 2003 (gmt 0)

10+ Year Member



I think you may need to say

document.adLink1.href=lnks1[currentAd1]

The way the code is right now, adLink1 should be a variable in the Javascript, when in fact it's an element in the page.

tcustom

7:02 pm on Dec 30, 2003 (gmt 0)

10+ Year Member



Thanks, but it looks like it may not support external images. You have to have your images stored on your own server.

I using external links and images for the banners since they are part of an ad campaign....

So here is what I need:

1) A rotator that will rotate banners (468x60) on a time you set (not one banner per refresh or going to another page on the site);
2) Support external images and URL links;
3) Supported by IE and Netscape
4) WISH LIST - Keeps stats on clicks, etc. (not necessary for now).

The one I am using above does what I need (except #4), but the banners will not rotate in Netscape 4.78 and up. The first banner just stays. IE works great...

You can see here: www.tcustomgolf.com
then look near the bottom of any page.

Thanks to all....

tcustom

7:06 pm on Dec 30, 2003 (gmt 0)

10+ Year Member



Thanks garann, I'll try it out...

tcustom

7:16 pm on Dec 30, 2003 (gmt 0)

10+ Year Member



garann,

Funny, but adding the document. to the line didn't fix Netscape, but made IE do the same thing Netscape is doing (showing only 1 banner and not rotating)....?

webkid

2:20 pm on Dec 31, 2003 (gmt 0)

10+ Year Member



Hi...

************
var imgs1 = new Array("image1.jpg","image2.gif","etc...")
var lnks1 = new Array("link1","link2", "etc....")
var alt1 = new Array("image1text","image2text","etc..")
var currentAd1 = 0
var imgCt1 = 3
function cycle1() {
currentAd1++
if (currentAd1 == imgCt1) {
currentAd1 = 0
}
document.adBanner1.src=imgs1[currentAd1]
document.adBanner1.alt=alt1[currentAd1]
adLink1.href=lnks1[currentAd1]
setTimeout("cycle1()",10 * 1000)
}

************

May change
"var imgs1 = new Array("image1.jpg","image2.gif","etc...")"
-->
var imgs1 = new Array("images/image1.jpg","images/image2.gif","images/image3.gif")

May change "var imgCt1 = 3"
--> var imgCt1 = imgs1.length

From the funtion cycle1()...
-->
function cycle1(){
if (document.images){
if document.adBanner1.complete){
currentAd1++
if (currentAd1 == imgCt1) {
currentAd1 = 0
}
document.adBanner1.src=imgs1[currentAd1]
}
setTimeout("cycle1()", 3 * 1000)
}
}

// add the new location function for the links
function newLocation(){
document.location.href = "http://www." +
alt1[currentAd1]
}

From the Page...
-->

<body onload="cycle1()">

anywhere if you like the banners go...

<p>
<center>
<a href="javascript:newLocation()"><img src="images/images1.jpg" width="..." height="..." name="adBanner1" border="0" alt="..."></a>
</center>

#######

.... hope this help... a bit long but if works then end of the year and welcome the New Year 2004 :)~

webkid

p.s. Also check the book from the website:
[javascriptworld.com...]
chapter3 the example: [javascriptworld.com...]

tcustom

2:34 pm on Dec 31, 2003 (gmt 0)

10+ Year Member



Webkid.....no problem being long. The problem has just been a nuisance to me...will give it a shot!

Thanks much to all....and a safe New Year!

tcustom

4:00 pm on Dec 31, 2003 (gmt 0)

10+ Year Member



webkid.....1 problem, can we get the below code for the new location to open in a new window? When clicking on the banners, I'd like to have the links open in a new window...

// add the new location function for the links
function newLocation(){
document.location.href = "http://www." +
alt1[currentAd1]
}

Putting target="_blank" after the call in my html page doesn't seem to work.

Took some of your code, and the code from the link you gave me and besides the above, it works well..

Thanks again...

webkid

11:51 pm on Jan 1, 2004 (gmt 0)

10+ Year Member



Sounds good:)

<a href="javascript:newLocation()"><img src="images/images1.jpg" width="..." height="..." name="adBanner1" border="0" alt="..."></a>

did you try target="_blank" there?

<a href="javascript:newLocation()" target="_blank">...

HTH.. :)

webkid

12:43 am on Jan 2, 2004 (gmt 0)

10+ Year Member



ok solve the problem...

function newLocation() {
//document.location.href = "http://www." + adURL[thisAd]
window.open("http://www." + adURL[thisAd],"my_new_window","toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=yes, copyhistory=yes, width=400, height=400")

}

***********
Use window.open, no need document.location.href...

It works just fine :)

HTP and Happy 2004

webkid

tcustom

2:53 pm on Jan 2, 2004 (gmt 0)

10+ Year Member



webkid...it works great....thanks for taking the time and enjoy the new year!