Re: Background images on the fly

by "Marc David Johnson" <mjohnson(at)marcdavidjohnson.com>

 Date:  Wed, 8 Aug 2001 12:34:39 -0400
 To:  "Brian Blackett" <bwblackett(at)yahoo.com>,
<hwg-techniques(at)hwg.org>
 References:  yahoo
  todo: View Thread, Original
From: "Brian Blackett" <bwblackett(at)yahoo.com>
> Can anyone tell me how I can change this image on the fly (using
Javascript) ?

Brian,

Try this:

In the <head>...</head> section of your document add:

<script language="JavaScript">
var backI = new Array();
backI[0] = "red.gif";
backI[1] = "blue.gif";
backI[2] = "";
function changeI(ImageName){
if (document.body){
document.body.background = backI[ImageName];
}}
</script>

and then change your body tag to:

<body onLoad="javascript:changeI(0)">

This will load the a file called red.gif as your background. Changing the 0
to a 1 in the onLoad event would change it to blue. You can add as many
elements as you like (right now setting the onLoad to 2 will give you a
blank - i.e. no -background) by adding 'backI[#] = ' array elements to the
Javascript code.

Now you just have to decide how you want to change the background (randomly,
on a user click, by reading a cookie the visitor sets, etc.). but it should
be easy as you only have to set what number from the array you want. :-)

HTH,

Marc David Johnson
mjohnson(at)marcdavidjohnson.com
http://www.MarcDavidJohnson.com
A little bit of everything ... and a whole lot of nothing.
http://mdj.inc <--- New.net address (info - v)
(http://www.qksrv.net/click-714870-1933347)

HWG hwg-techniques mailing list archives, maintained by Webmasters @ IWA