Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Random background  (Read 10573 times)

0 Members and 1 Guest are viewing this topic.

sjkastronomy

  • Guest
Random background
« on: July 15, 2004, 05:40:33 AM »
Hi!,

I have searched very long to a working html or javascript code for a random background on my website.
My website uses frames as you can see. Please take a look at my website>>

http://v4.livegate.net/sjkastronomy/home.html

I want a random background in the navigator bar at the left hand side.

As you typ in http://v4.livegate.net/sjkastronomy/image1.jpg
in the browser, the background is displayed. I want that background and 9 others, randomly displayed on that navigator bar.

I already have put a code on my website, as it was told to do >>
http://www.groan-zone.net/jscript/random.html

When I install this code on my website (offline), it works!
I used this code between the <head></head> tags:

<script language="JavaScript">
<!-- Activate cloaking device
var randnum = Math.random();
var inum = 10;
// Change this number to the number of images you are using.
var rand1 = Math.round(randnum * (inum-1)) + 1;
images = new Array
images[1] = "image1.jpg"
images[2] = "image2.jpg"
images[3] = "image3.jpg"
images[4] = "image4.jpg"
images[5] = "image5.jpg"
images[6] = "image6.jpg"
images[7] = "image7.jpg"
images[8] = "image8.jpg"
images[9] = "image9.jpg"
images[10] = "image10.jpg"
// Ensure you have an array item for every image you are using.
var image = images[rand1]
// Deactivate cloaking device -->
</script>

and this code in stead of the <body> tag

<script language="JavaScript">
<!-- Activate cloaking device
document.write('<body background="' + image + '" text="white">')
// Deactivate cloaking device -->
</script>

When I upload the panel.html, with the installed code, I does not work.

I also tried to change the code as you can see here:
((with the full image adress behind the images "http://v4.livegate.net/sjkastronomy/"))

<script language="JavaScript">
<!-- Activate cloaking device
var randnum = Math.random();
var inum = 10;
// Change this number to the number of images you are using.
var rand1 = Math.round(randnum * (inum-1)) + 1;
images = new Array
images[1] = "http://v4.livegate.net/sjkastronomy/image1.jpg"
images[2] = "http://v4.livegate.net/sjkastronomy/image2.jpg"
images[3] = "http://v4.livegate.net/sjkastronomy/image3.jpg"
images[4] = "http://v4.livegate.net/sjkastronomy/image4.jpg"
images[5] = "http://v4.livegate.net/sjkastronomy/image5.jpg"
images[6] = "http://v4.livegate.net/sjkastronomy/image6.jpg"
images[7] = "http://v4.livegate.net/sjkastronomy/image7.jpg"
images[8] = "http://v4.livegate.net/sjkastronomy/image8.jpg"
images[9] = "http://v4.livegate.net/sjkastronomy/image9.jpg"
images[10] = "http://v4.livegate.net/sjkastronomy/image10.jpg"
// Ensure you have an array item for every image you are using.
var image = images[rand1]
// Deactivate cloaking device -->
</script>

But it still does not work!

Can somebody please help me??

I searched very long for the code, but I still could not find it. I think it does not work, because my website uses frames. But I don't know and could not fnd the proper code for it.

Sander Klieverik

sjkastronomy

  • Guest
Re: Random background
« Reply #1 on: July 15, 2004, 10:48:57 AM »
Hi!,

I have found the solution. Thanks for everybody reading this topic. For other people, sharing the same problem, you can see what you have to do with a random background javascript code, when your website contains frames.

This is the code you must use between the <head></head> tags:

<script>
if (parent.frames.length==0)
window.location.replace("index.html")
</script>
<script type="text/javascript">
onload = function() {
     // set up an array of the images
     images = new Array("image1.jpg", "image2.jpg","image3.jpg","image4.jpg","image5.jpg","image6.jpg","image7.jpg","image8.jpg","image9.jpg","image10.jpg");
     rand = Math.floor(Math.random()*images.length); // choose a random number, between 0 and the length of the array -1
     img = images[rand] // set img to the random image's src
     document.body.style.background= "url("+img+")"; // set the background image
     }
</script>

Sander Klieverik