Log in

View Full Version : Random Background



nick019
03-24-2006, 05:18 AM
Hi,
I was just wondering if anybody knew how the backgrounds on Popstar Madonna's site keeps changing? Is it a Javascript or something else? I just have to know!
Thankyou to anyone that can help me!:)

P.S. The site is http://madonna.com/ and is best viewed in Internet Explorer or Mozilla Firefox!:)

Samsoske
03-24-2006, 05:33 AM
Here, this should help:

place this in the <head> section of your page:



<script language="javascript">
<!--
bg = new Array()
bg[0] = ("firstimage.gif")
bg[1] = ("secondimage.jpg")
bg[2] = ("third.gif")
bg[3] = ("fourth.gif")
bg[4] = ("fifth.gif")
bg[5] = ("sixth.gif")
bg[6] = ("seventh.jpg")
bg[7] = ("eighth.gif")
var Numb = Math.round(7 * Math.random());
var back = bg[Numb]
document.write("<body bgcolor='#000000' text='#ffd700'link='#ffa500' vlink='#cc0000' alink='#ffa500' background=" +back+ ">")
--> </script>


Just replace those above images with those from you directory..

bye.

nick019
03-24-2006, 05:41 AM
OMG, Thankyou sooooo much:D I appreciate this heaps... I couldn't find this anywhere! The closest thing was a random color changer!
Thank You!!

Samsoske
03-24-2006, 09:07 PM
It was a pleasure doing Buisness!

Glad I could help:p

Bye.

djr33
03-25-2006, 11:22 AM
Hmm.... you'd need to remember to not include an extra <body>, right?
And wouldn't you want the script below the </head> tag so it replaces the body tag at the right point?

Samsoske
03-25-2006, 06:13 PM
Hi there...\


Originally posted by djr33
Hmm.... you'd need to remember to not include an extra <body>, right?
And wouldn't you want the script below the </head> tag so it replaces the
body tag at the right point?


No. You would lay out your page, just like you would if you were making a
site. And that means you include the <body> tag. Because without this
<tag> the web site won't function.


Another: you add the code to the <head> section of you source, just dont
specify any value for a bgcolor, or background in the original <body> attribute.

All this code does, is overide the body's bgcolor function, if you don't put
one, and display the images out in the array, on random turns. Simple.

Twey
03-25-2006, 06:53 PM
Actually, djr33 is right. Using this script with a normally-laid-out site, you're going to end up with something that looks like this (after the script has run):
<head>
<body bgcolor='#000000' text='#ffd700'link='#ffa500' vlink='#cc0000' alink='#ffa500' background='seventh.gif'>
</head>
<body>
... rest of page ...This is not well-formed HTML. One solution would be, as djr33 suggested, to put the script in the place of the <body> tag. However, this will destroy the page in non-JS browsers. The correct way to do this would be:
<head>
<title></title>
<script type="text/javascript">
onload = function() {
bg = new Array("firstimage.gif", "secondimage.jpg", "thirdimage.png", "fourthimage.tif"/*, ... */);
var Numb = Math.floor(bg.length * Math.random());
document.body.style.backgroundImage="url(" + bg[Numb] + ")";
};
</script>
</head>