On Tue, 10 Aug 2004 17:06:01 GMT, david.graham18
<da************@ntlworld.com> wrote:
The method was to select a number at random from 1 to 4 by usingthe
rnd() function and then dividing by modulus 4.
Wouldn't it be more sensible to generate a value between zero and three?
The result was then used to select a cell in a 4 cell array whichwas
holding a different colour in each cell of the array. [...]
var changeBackground = (function() {
var colours = ['#ffffff', '#000000'];
var b = null, s = null;
function random(n) {
return Math.floor(n * Math.random());
}
if((b = document.body) && (s = b.style) &&
'string' == typeof s.backgroundColor)
{
return function() {
s.backgroundColor = colours[random(colours.length)];
};
} else {
return function() {};
}
})();
This creates a function, changeBackground(), that will randomly select a
colour in the colours array and use it to change the background colour for
the document body. You can add as many colours as you want to the array.
Just make sure they're a valid CSS colour value[1], and they leave the
page readable (though that goes without saying).
If you want to change the background just once, you could simplify the
above to:
(function() {
var colours = ['#ffffff', '#000000'];
var b = null, s = null;
function random(n) {
return Math.floor(n * Math.random());
}
if((b = document.body) && (s = b.style) &&
'string' == typeof s.backgroundColor)
{
s.backgroundColor = colours[random(colours.length)];
}
})();
Hope that helps,
Mike
[1] See the CSS specification for valid values
<URL:http://www.w3.org/TR/REC-CSS2/syndata.html#color-units>.
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail