PDA

View Full Version : banner change


kenny873
02-05-2003, 03:45 AM
how can i rotate different banners in one place???

whammy
02-05-2003, 04:50 AM
Simple way:


<html>
<head>
<title>Looping OR Random banner example</title>
<script type="text/javascript">
<!--
var i = 0;
function getBanner() {
var bannerstring = "banner1.jpg,banner2.jpg,banner3.jpg,banner4.jpg";
var banners = bannerstring.split(",");

/* RANDOM BANNER SECTION (commented out) */
// var whichbanner = Math.floor(Math.random() * banners.length);
// document.images.bannerpic.src = banners[whichbanner];
/* END SECTION */

/* SEQUENTIAL BANNER SECTION */
document.images.bannerpic.src = banners[i];
i++;
if(i == banners.length) i = 0;
/* END SECTION */

/* Don't mess with this! */
setTimeout('getBanner()',3000); // 3000 = 3 seconds
}
// -->
</script>
</head>
<body onload="getBanner()">

<img src="blah.jpg" id="bannerpic" />

</body>
</html>


As you can see, there are a couple of options there, I have the random banner code commented out. I just wrote this on the fly, but it's pretty dynamic since all you'd have to do to add or remove banners is add them to the variable "bannerstring", comma-delimited as shown.

Not sure about Netscape 4 compatibility, you might need to add name="bannerpic" to the img src for that... ?

Hope this helps. :)