I'm making a new website & I want to know if it’s possible for the background colour to be randomly selected from a list of hex codes? So every time you go to the page there’s a different background colour.
Thanks in advance.![]()
I'm making a new website & I want to know if it’s possible for the background colour to be randomly selected from a list of hex codes? So every time you go to the page there’s a different background colour.
Thanks in advance.![]()
Be careful that there's no text directly on top of this colour, though, or it might be unreadable sometimes. Also, don't forget users without scripting support -- always provide a default background colour or, if possible, generate the random colour server-side.Code:<script type="text/javascript" src="http://www.twey.co.uk/pythonic.js"></script> <script type="text/javascript"> Twey.Pythonic.LOAD("Array.randomChoice"); document.body.style.backgroundColor = "#" + [ "fff", "000", "00f", "0ff", "0f0", "fff000" ].randomChoice(); </script>
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
This cannot be done with just HTML.
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
I don't think loading an external script is really worth the hassle for such a simple script.
Having the script in the page eliminates the hotlinking to your own site and allows the coder to find out whats actually happening easier. (Twey, your external code is too sophisticated for begginers to understand.)
Code:<script type="text/javascript"> colors = new Array(); colors[0] = "#FF0000"; colors[1] = "#FFCC00"; colors[2] = "#00FF00"; colors[3] = "#0000FF"; colors[4] = "#00FFCC"; colors[5] = "#CCFF00"; //etc randomNumber = Math.floor(Math.random()*colors.length); document.body.style.backgroundColor = colors[randomNumber]; </script>![]()
Last edited by Bob90; 05-20-2007 at 11:05 AM. Reason: added script type
Mm, probably true.I don't think loading an external script is really worth the hassle for such a simple script.They could always download it, or even copy-and-paste it into the script wholesale. It's only there for convenience.Having the script in the page eliminates the hotlinking to your own site and allows the coder to find out whats actually happening easier.The type attribute is required.<script>That's a lot of assignments. I think it would be more efficient and certainly easier to use an array literal. There's also no need for all those globals; to avoid cluttering the global namespace, perhaps wrap it in a new scope:colors = new Array();
colors[0] = "#FF0000";
colors[1] = "#FFCC00";
colors[2] = "#00FF00";
colors[3] = "#0000FF";
colors[4] = "#00FFCC";
colors[5] = "#CCFF00";Code:<script type="text/javascript"> (function() { var colours = [ "#FF0000", "#FFCC00", "#00FF00", "#0000FF", "#00FFCC", "#CCFF00" ]; document.body.style.backgroundColor = colours[Math.floor(Math.random() * colours.length)]; })(); </script>
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
So colours doesn't end up global. I'm not entirely sure if the cost of creating and running a function is worth it, but John reckon(s|ed) that it is.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Very interesting points.
I didn't think about global declaration, I was just supplying the level of solution I thought met the request (i.e new coder requesting something very easy; they obviously need an introduction to most things.)
![]()
I see.So colours doesn't end up global. I'm not entirely sure if the cost of creating and running a function is worth it, but John reckon(s|ed) that it is.
Yet there's one "bug" in your code. If it's placed into the head section, then it's not gonna work (document.body...), however document.bgColor works fine.
The bgColor property is deprecated for CSS. I was intending for it to be placed in the body, yes; I probably should have made that clearer.Yet there's one "bug" in your code. If it's placed into the head section, then it's not gonna work (document.body...), however document.bgColor works fine.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Bookmarks