473,387 Members | 1,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

changing background colour

Hi
I spotted some nice code to change the background colour of a web page to
one of four different colours at random but I can't find it now!

The method was to select a number at random from 1 to 4 by using the rnd()
function and then dividing by modulus 4. The result was then used to select
a cell in a 4 cell array which was holding a different colour in each cell
of the array. I would like to have this code but lack the knowledge to
re-create it. Anyone able to do this please.

thanks
David
Jul 23 '05 #1
4 1947
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
Jul 23 '05 #2

"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:opscizrndrx13kvk@atlantis...
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?

Hi
Thanks for the reply, I will try to understand your code. On the numbers 1
to 4 thing, dividing by modulus 4 will only produce results 0, 1, 2 or 3 - I
think. I might have caused confusion by stating it produces numbers 1 to 4

thanks
David
Jul 23 '05 #3
On Tue, 10 Aug 2004 18:48:27 GMT, david.graham18
<da************@ntlworld.com> wrote:

[snip]
Thanks for the reply, I will try to understand your code.
If there's anything in particular you want to know, do ask.
On the numbers 1 to 4 thing, dividing by modulus 4 will onlyproduce
results 0, 1, 2 or 3 - I think. I might have causedconfusion by stating
it produces numbers 1 to 4


Indeed it will. The modulus operator returns the remainder after a
division, so:

1 % 4 = 0 r 1
2 % 4 = 0 r 2
3 % 4 = 0 r 3
4 % 4 = 1 r 0
etc.

However, it's much simpler (and more efficient) to produce 0..3 from the
offset.

Good luck,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail
Jul 23 '05 #4

"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:opscjkq7mfx13kvk@atlantis...
On Tue, 10 Aug 2004 18:48:27 GMT, david.graham18
<da************@ntlworld.com> wrote:

[snip]
Thanks for the reply, I will try to understand your code.

Hi
I need to learn more Javascript to be able to understand your code. I have
only a very basic understanding at the moment. You would have to explain
each line of your code for me to understand it - I don't expect you to go to
that sort of trouble, but your help is most appreciated. I suppose I liked
the simple version of a random colour picker as it was easy to get to grips
with, I've no doubt that your version is far supperior but unfortunately it
is too difficult for me.

thanks
David
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John D | last post by:
I am trying to change the background colour of either a div or a textarea with javascript, but am having problems. I have passed a hex colour value in the variable hval, and with the following...
14
by: Holden Caulfield | last post by:
Hi there, for IE 5+ I need to be able to change certain TD tagsī background colors, and canīt figure out how to do it... For instance, in a table with 25 cells, somewhere between 5 or 10 will...
4
by: Dj Frenzy | last post by:
Hi, I know how to use javascript to change a background image to another background image, and how to change a background colour to another background colour. Is there a way to change an image to a...
13
by: mike | last post by:
I have some elements that I want to change color when I mouse over them and them back on mouse off. I'd like the color to remain with "#c7d0e0" when the user clicks on them. <td...
3
by: Peter Williams | last post by:
Hi All, I want to write some javascript for a html page which does the following. Imagine that the page contains a table with 2 columns and 3 rows, e.g.: +---+---+ | A | B | +---+---+
25
by: madsgormlarsen | last post by:
Hi I am making a tab menu with css, and the tabs changes color depending on wich tab is current. 1. I could load a different css file for each tab/color. 2. I could do some inline css only...
2
by: Colin McGuire | last post by:
Hi, according to the on-line documentation you can change the background colour of, for example a button, to Gray as shown below. Dim b as Button b.BackColor=Color.Gray If I programmatically...
5
by: kaisersose1995 | last post by:
Hi, I am trying to add some visual confirmation to a continous form, to let the user know if a stored date on the form has passed. I have managed to set the background colour using a simple IF...
3
by: bettyboo | last post by:
Hi I'm new to the forum and also a VERY new user of Access to develop databases. I'm building a DB for a driving instructor acquaintance, and he wants a button on the pupil data entry form which...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.