Click to See Complete Forum and Search --> : random image background
georgeafi
03-22-2003, 07:47 PM
Ok hopefully i won't need any help from you guys abter this. Right now i have a background image on my site and it is of a member of the band my site is about. What i want is to have a 1 of 4 pics randomly come up when the page is entered or refreshed. I have seen this before so i know it can be done. I just can't find it on htmlgoodies.com or echoecho.com. Hopefully someone has done this and knows how to do it. The only thing i have found is a random background color.
khaki
03-23-2003, 11:05 AM
Hi George (again!)...
The site looks a lot better since the last time I viewed it (no more whaky text when using Netscape. yay!).
To answer your question, here is a background image changer from Javascript.com:
http://javascript.internet.com/bgeffects/r-images.html
I'm not sure what this would do if a visitor has Javascript disabled (since the <body> is written client-side).
Also, it seems that the random number process could be a bit simpler (but that is definitely not my area of expertise). I'm just thinking that maybe it could be simplified (if someone - not me! - knew how to do such things. lol)
Hope this helps... and I'm glad that things are progressing for you.
rock on...
k
Nedals
03-23-2003, 11:41 AM
Try this...
<html><head><title>Untitled</title>
<script type="text/javascript">
<!--
var imgAry = new Array('sample1.jpg','sample2.jpg','sample3.jpg','sample4.jpg')
var randomNumber = Math.floor(Math.random()*3); // creates a ramdom number between 1 and 4 (0 and 3)
document.open();
document.write("<body background=\"" +imgAry[randomNumber]+ "\">");
//-->
</script>
</head>
<body bgcolor="#ffffff">
</body>
</html>
Update:
I just noticed a small error. The random number should be 0 to 3
khaki
03-23-2003, 12:02 PM
WOW Nedals...
That's SWEEEEEET!!!
(it's strange - well.... maybe not - but it seems that so many of the scripts from Javascript.com are really just too bloated and complex. No wonder I can't learn from that stuff!)
I understand this one though!
Thanks Nedals!
k
georgeafi
03-23-2003, 03:06 PM
Hmmmmmm I tried both of those and neither one worked. The link that Khaki gave me i didn't really like cause you have to hit a button for the image to change. And the script that Nedals posted didn't work either. I don't know why it didn't cause i could somewhat understand what would do what. But thanks for trying to help. I'll be patient and mabey someone will find something. :D
khaki
03-23-2003, 03:15 PM
Hi George...
I can understand not liking the linked script (it only uses the button for the example y'know... the script actually works onLoad or refresh).
Nedals version works great! You must be doing something wrong.
Post how you tried to use it...
(did you alter the * 3 part to match the number of images in your array? Remember... 0 counts too)
confused...
k
georgeafi
03-23-2003, 03:23 PM
Nevermind i just got it to work like 5 seconds ago. Thanks again you guys. Now i just need to get my links up on everpage. :D :D :D