| | |
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 |
Try this:
HTML Syntax (Toggle Plain Text)
<html> <head> <script type="text/javascript"> function randomize(min, max) { if (!min) min = 0; if (!max) max = 1; return Math.floor(Math.random()*(max+1)+min); } function randomBg() { var bgs = new Array(); // I took these images from a site with a similar script already only to get example images // I wrote this script myself bgs.push("http://javascript.internet.com/img/backgrounds/wood.gif"); bgs.push("http://javascript.internet.com/img/backgrounds/paper.gif"); bgs.push("http://javascript.internet.com/img/backgrounds/clouds.gif"); bgs.push("http://javascript.internet.com/img/backgrounds/faces.gif"); bgs.push("http://javascript.internet.com/img/backgrounds/marble.gif"); document.body.style.backgroundImage = "url(" + bgs[randomize(0, bgs.length-1)] + ")"; } </script> </head> <body onLoad="randomBg()" style="text-align:center;"> <span style="font-size:1.5em; background-color:white; border:1px dashed black; padding:5px;">A random background image should appear!</span> </body> </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
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
You're going to have something like this:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
var srcs = {"images/pic1","images/pic2","images/pic3"}; var index= Math.floor ( Math.random() * 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
If you found a post helpful, please click the "give XXX reputation" link, to show your appreciation to those who helped you. Thanks! :D
•
•
Join Date: May 2009
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
Try this:
HTML Syntax (Toggle Plain Text)
<html> <head> <script type="text/javascript"> function randomize(min, max) { if (!min) min = 0; if (!max) max = 1; return Math.floor(Math.random()*(max+1)+min); } function randomBg() { var bgs = new Array(); // I took these images from a site with a similar script already only to get example images // I wrote this script myself bgs.push("http://javascript.internet.com/img/backgrounds/wood.gif"); bgs.push("http://javascript.internet.com/img/backgrounds/paper.gif"); bgs.push("http://javascript.internet.com/img/backgrounds/clouds.gif"); bgs.push("http://javascript.internet.com/img/backgrounds/faces.gif"); bgs.push("http://javascript.internet.com/img/backgrounds/marble.gif"); document.body.style.backgroundImage = "url(" + bgs[randomize(0, bgs.length-1)] + ")"; } </script> </head> <body onLoad="randomBg()" style="text-align:center;"> <span style="font-size:1.5em; background-color:white; border:1px dashed black; padding:5px;">A random background image should appear!</span> </body> </html>
![]() |
Similar Threads
- javascript css background-image (JavaScript / DHTML / AJAX)
- specifying the background image for a div using a text box (JavaScript / DHTML / AJAX)
- Setting a Background Image problem (PHP)
- Random Background image (HTML and CSS)
- background image alignment (HTML and CSS)
- background image - help (HTML and CSS)
- problem with background image repeat-y (HTML and CSS)
- Background image flickers in IE.... (JavaScript / DHTML / AJAX)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Ajax Ifram Auto Resize freezing with IE ...
- Next Thread: how do i add javascript to a form that i made that is all php?
| Thread Tools | Search this Thread |
acid2 ajax ajaxexample ajaxjspservlets array browser bug captchaformproblem cart checkbox child class close codes cookies createrange() css cursor date debugger decimal dependent design disablefirebug dom dropdown editor element embed engine enter events explorer ext file firefox focus form forms frameworks getselection google gxt hiddenvalue highlightedword hint html ie7 ie8 iframe images index internet java javascript javascripthelp2020 jquery jsf jsfile jsp jump libcurl listbox maps masterpage math media menu mp4 object onmouseoutdivproblem onmouseover onreadystatechange parent paypal pdf php position post programming progressbar prototype redirect runtime safari scale scriptlets scroll search security session shopping size software toggle unicode variables web wysiwyg \n





