Shinji Posted February 9, 2008 Share Posted February 9, 2008 Hey im wondering how i can have a bunch of random backgrounds for a webpage. Here is what i have so far, and for me its not workin.for the meantime im just trying to get it to work so i have one image called bg1.png and i figure i just need to get the random number deal working and them make more backgrounds <script type="text/javascript"><!--document.write('<body background="bg' + Math.floor(Math.random()*1) + '.png">');--></script> anyone help? Link to comment Share on other sites More sharing options...
Synook Posted February 9, 2008 Share Posted February 9, 2008 Try to use more modern standards such as CSS. <head><!-- ... --><script type="text/javascript">window.onload = function() {document.getElementsByTagName("body")[0].style.backgroundImage = "url(bg" + (Math.floor(Math.random()*1) + 1) + ".png)";}</script></head><body> Link to comment Share on other sites More sharing options...
jeffman Posted February 9, 2008 Share Posted February 9, 2008 Hate to be persnickety, gang, but this bit will get you 0 every time:Math.floor(Math.random()*1)No doubt you're just re-pasting a typo that should read as follows:Math.floor(Math.random()*10)or maybeMath.floor(Math.random()*100)depending on the number of images you have.Cheers! Link to comment Share on other sites More sharing options...
Shinji Posted February 9, 2008 Author Share Posted February 9, 2008 Thanks all! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.