Random Background Image Script

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: May 2009
Posts: 6
Reputation: JimPatrick is an unknown quantity at this point 
Solved Threads: 0
JimPatrick JimPatrick is offline Offline
Newbie Poster

Random Background Image Script

 
0
  #1
May 26th, 2009
Hi I am looking for a random background image script. I have 3 background images that cover the whole browser and I want them to rotate when someone hits the refresh button on the browser. Can anyone help me on this thanks.
JimPatrick
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 97
Reputation: itsjareds is an unknown quantity at this point 
Solved Threads: 12
itsjareds's Avatar
itsjareds itsjareds is offline Offline
Junior Poster in Training

Re: Random Background Image Script

 
0
  #2
May 26th, 2009
Try this:
  1. <html>
  2. <head>
  3.  
  4. <script type="text/javascript">
  5. function randomize(min, max) {
  6. if (!min)
  7. min = 0;
  8. if (!max)
  9. max = 1;
  10. return Math.floor(Math.random()*(max+1)+min);
  11. }
  12. function randomBg() {
  13. var bgs = new Array();
  14.  
  15. // I took these images from a site with a similar script already only to get example images
  16. // I wrote this script myself
  17. bgs.push("http://javascript.internet.com/img/backgrounds/wood.gif");
  18. bgs.push("http://javascript.internet.com/img/backgrounds/paper.gif");
  19. bgs.push("http://javascript.internet.com/img/backgrounds/clouds.gif");
  20. bgs.push("http://javascript.internet.com/img/backgrounds/faces.gif");
  21. bgs.push("http://javascript.internet.com/img/backgrounds/marble.gif");
  22.  
  23. document.body.style.backgroundImage = "url(" + bgs[randomize(0, bgs.length-1)] + ")";
  24. }
  25. </script>
  26.  
  27. </head>
  28. <body onLoad="randomBg()" style="text-align:center;">
  29.  
  30. <span style="font-size:1.5em; background-color:white; border:1px dashed black; padding:5px;">A random background image should appear!</span>
  31.  
  32. </body>
  33. </html>
Last edited by itsjareds; May 26th, 2009 at 10:23 pm.
Mark your post as Solved if you've been helped!

I learn by helping others learn.
If you find one of my posts helpful, don't be afraid to give me a reputation bump :D
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 119
Reputation: JugglerDrummer is an unknown quantity at this point 
Solved Threads: 15
JugglerDrummer's Avatar
JugglerDrummer JugglerDrummer is offline Offline
Junior Poster

Re: Random Background Image Script

 
0
  #3
May 26th, 2009
You're going to have something like this:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var srcs = {"images/pic1","images/pic2","images/pic3"};
  2. var index= Math.floor ( Math.random() * 3 );
  3. imagethingy.src = srcs[index];
92% of all statistics are made up on the spot.

If you found a post helpful, please click the "give XXX reputation" link, to show your appreciation to those who helped you. Thanks! :D
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 6
Reputation: JimPatrick is an unknown quantity at this point 
Solved Threads: 0
JimPatrick JimPatrick is offline Offline
Newbie Poster

Re: Random Background Image Script

 
0
  #4
May 26th, 2009
Thanks mate I am just testing your script right now and how I can implement it on my site right now thanks will keep u posted if I need further help.
JimPatrick
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 6
Reputation: JimPatrick is an unknown quantity at this point 
Solved Threads: 0
JimPatrick JimPatrick is offline Offline
Newbie Poster

Re: Random Background Image Script

 
0
  #5
May 26th, 2009
Originally Posted by itsjareds View Post
Try this:
  1. <html>
  2. <head>
  3.  
  4. <script type="text/javascript">
  5. function randomize(min, max) {
  6. if (!min)
  7. min = 0;
  8. if (!max)
  9. max = 1;
  10. return Math.floor(Math.random()*(max+1)+min);
  11. }
  12. function randomBg() {
  13. var bgs = new Array();
  14.  
  15. // I took these images from a site with a similar script already only to get example images
  16. // I wrote this script myself
  17. bgs.push("http://javascript.internet.com/img/backgrounds/wood.gif");
  18. bgs.push("http://javascript.internet.com/img/backgrounds/paper.gif");
  19. bgs.push("http://javascript.internet.com/img/backgrounds/clouds.gif");
  20. bgs.push("http://javascript.internet.com/img/backgrounds/faces.gif");
  21. bgs.push("http://javascript.internet.com/img/backgrounds/marble.gif");
  22.  
  23. document.body.style.backgroundImage = "url(" + bgs[randomize(0, bgs.length-1)] + ")";
  24. }
  25. </script>
  26.  
  27. </head>
  28. <body onLoad="randomBg()" style="text-align:center;">
  29.  
  30. <span style="font-size:1.5em; background-color:white; border:1px dashed black; padding:5px;">A random background image should appear!</span>
  31.  
  32. </body>
  33. </html>
itsjareds - thank you so much I have manage to fix it now its working wonders mate. Much appreciated.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 6
Reputation: JimPatrick is an unknown quantity at this point 
Solved Threads: 0
JimPatrick JimPatrick is offline Offline
Newbie Poster

Re: Random Background Image Script

 
0
  #6
May 26th, 2009
Thanks guys for your kindness, truly only by giving to we live. Thank u for sharing with me ur skills.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC