twilitegxa Posted June 20, 2009 Share Posted June 20, 2009 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> Quote Link to comment Share on other sites More sharing options...
fnairb Posted June 20, 2009 Share Posted June 20, 2009 I believe that Pic.length should be (Pic.length -1) Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted June 22, 2009 Author Share Posted June 22, 2009 That didn't seem to fix the problem. :-( Any other suggestions? Quote Link to comment Share on other sites More sharing options...
fnairb Posted June 23, 2009 Share Posted June 23, 2009 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. Quote Link to comment Share on other sites More sharing options...
xtopolis Posted June 23, 2009 Share Posted June 23, 2009 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. Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted July 1, 2009 Author Share Posted July 1, 2009 I would love a php example :-) Whichever way would work best. Currently, I'm only using this as a desktop changing background, but I would still love the knowledge. Thanks :-) Quote Link to comment Share on other sites More sharing options...
xtopolis Posted July 3, 2009 Share Posted July 3, 2009 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.