Jump to content

Image Changing Backgroung


twilitegxa

Recommended Posts

I have the following code, that seems to work, but occasionally the background image is a blank white page instead of an image in my list. Can anyone tell me what I'm doing wrong? Here is the code:

 

<HTML>
<HEAD>
</HEAD>
<STYLE>
</STYLE>
<SCRIPT LANGUAGE = "JavaScript">
function Background()
{
var rand = Math.round(Math.random()*2);
if(rand==0)
{
AhMyGoddess();
}
if(rand==1)
{
Chobits();
}
if(rand==2)
{
Lunar();
}
}
function AhMyGoddess()
{
var Pic = new Array()
{
Pic[0] = 'AMG1.jpg';
Pic[1] = 'AMG2.jpg';
Pic[2] = 'AMG3.jpg';
Pic[3] = 'AMG4.jpg';
Pic[4] = 'AMG5.jpg';
Pic[5] = 'AMG6.jpg';
Pic[6] = 'AMG7.jpg';
Pic[7] = 'AMG8.jpg';
Pic[8] = 'AMG9.jpg';
Pic[9] = 'AMG10.jpg';
Pic[10] = 'AMG11.jpg';
Pic[11] = 'AMG12.jpg';
Pic[12] = 'AMG13.jpg';
Pic[13] = 'AMG14.jpg';
Pic[14] = 'AMG15.jpg';
Pic[15] = 'AMG16.jpg';
Pic[16] = 'AMG17.jpg';
Pic[17] = 'AMG18.jpg';
Pic[18] = 'AMG19.jpg';
Pic[19] = 'AMG20.jpg';
Pic[20] = 'AMG21.jpg';
Pic[21] = 'AMG22.jpg';
Pic[22] = 'AMG23.jpg';
Pic[23] = 'AMG24.jpg';
Pic[24] = 'AMG25.jpg';
Pic[25] = 'AMG26.jpg';
Pic[26] = 'AMG27.jpg';
Pic[27] = 'AMG28.jpg';
Pic[28] = 'AMG29.jpg';
}
var j = Math.round(Math.random()*Pic.length);
document.body.background=Pic[j];
window.setTimeout('Background()', 25000);
}
function Chobits()
{
var Pic = new Array()
{
Pic[0] = 'C1.jpg';
Pic[1] = 'C2.jpg';
Pic[2] = 'C3.jpg';
Pic[3] = 'C4.jpg';
Pic[4] = 'C5.jpg';
}
var j = Math.round(Math.random()*Pic.length);
document.body.background=Pic[j];
window.setTimeout('Background()', 25000);
}
function Lunar()
{
var Pic = new Array()
{
Pic[0] = 'EB1.jpg';
}
var j = Math.round(Math.random()*Pic.length);
document.body.background=Pic[j];
window.setTimeout('Background()', 25000);
}
</SCRIPT>
<BODY onLoad="Background();">
</BODY>
</HTML>

Link to comment
Share on other sites

Throw in a debug statement.  Something like:

alert("Pic[" + j + "] => " + Pic[j]);

 

Then make sure the file exists exactly like the path from Pic[j].  (I have a habbit of occasionally putting .png when I mean .jpg)

 

It should give you the info to get you a long way to figuring out where the problem is.

Link to comment
Share on other sites

This code could be considerably shorter and more manageable if you used PHP instead.  If you'd like a PHP implementation, ask in this thread and I'll happily provide an example.  Even with your "change every 25 seconds" thing, you could cue up an array that would last however long you wanted it to.

 

Otherwise, you should refactor your javascript functions which do many of the same tasks in order to save code/headaches.

Link to comment
Share on other sites

  • 2 weeks later...

Been busy,

 

Where are you at on this?  As for the php example, it would simply be

-break down the images based on category (Chobits, AMG, etc) into separate folders

-scrape the directory based on the choice

-create a random array in php to echo as a javascript array

-javascript takes over from there

 

If you are still having trouble with Javascript for this, post what you have now and I can try to look at it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.