By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,903 Members | 1,647 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,903 IT Pros & Developers. It's quick & easy.

random bgcolor

P: n/a
hi!
how can i manage it (html or jscript with css) that everytime a user loads
or refreshes a page, the page has a new bgcolor.
i want to put it in a single file, so that all my pages have the same color,
but after every refresh a new random one.
thanx 4 your support

Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
TaTonka wrote on 18 sep 2004 in comp.lang.javascript:
how can i manage it (html or jscript with css) that everytime a user
loads or refreshes a page, the page has a new bgcolor.
can be done

<body onload='changeBgClr()'>
i want to put it in a single file, so that all my pages have the same
color, but after every refresh a new random one.


This is illogical.
You just asked for a new colour at every load or refresh.

========

<script type='text/javascript'>

function toHex(x) {
switch(x) {
case 10: return "A";
case 11: return "B";
case 12: return "C";
case 13: return "D";
case 14: return "E";
case 15: return "F";
}
return x;
}
c='#'
c+= toHex(Math.round(Math.random()*16));
c+= toHex(Math.round(Math.random()*16));
c+= toHex(Math.round(Math.random()*16));
c+= toHex(Math.round(Math.random()*16));
c+= toHex(Math.round(Math.random()*16));
c+= toHex(Math.round(Math.random()*16));

//alert(c)
document.body.style.backgroundColor=c

</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 23 '05 #2

P: n/a
JRS: In article <Xn********************@194.109.133.29>, dated Sat, 18
Sep 2004 15:51:36, seen in news:comp.lang.javascript, Evertjan.
<ex**************@interxnl.net> posted :
TaTonka wrote on 18 sep 2004 in comp.lang.javascript:
how can i manage it (html or jscript with css) that everytime a user
loads or refreshes a page, the page has a new bgcolor.
can be done

<body onload='changeBgClr()'>
i want to put it in a single file, so that all my pages have the same
color, but after every refresh a new random one.


This is illogical.
You just asked for a new colour at every load or refresh.

========

<script type='text/javascript'>

function toHex(x) {
switch(x) {
case 10: return "A";
case 11: return "B";
case 12: return "C";
case 13: return "D";
case 14: return "E";
case 15: return "F";
}
return x;
}


function toHex(x) { // uncomment one line; all untested
// return String.fromCharCode( x>9 ? x+55 : x+48 )
// return String.fromCharCode( x + [48,55][+(x>9)] )
// return String.fromCharCode( x + 48 + 7*(x>9) )
// return "0123456789ABCDEF".substr(x, 1)
// return x.toString(16)
}

c='#'
c+= toHex(Math.round(Math.random()*16));
c+= toHex(Math.round(Math.random()*16));
c+= toHex(Math.round(Math.random()*16));
c+= toHex(Math.round(Math.random()*16));
c+= toHex(Math.round(Math.random()*16));
c+= toHex(Math.round(Math.random()*16));


You mean floor, not round; and there is the Opera bug that means that
Math.random can give 1.0.

The components can be concatenated in a single statement. But I'd use a
loop.

Or :

c = ( "00000" + Math.floor( (Math.random()%1)*Math.pow(16,6) ).
toString(16) ).substr(-6) // substr untested
or

c = ( "00000" + Math.floor( (Math.random()%1)*Math.pow(16,6) ).
toString(16) ).replace(/(.*)(.{6})$/, '$2')
or

c = ( Math.pow(16,6) + Math.floor( (Math.random()%1)*Math.pow(16,6) ) ).
toString(16).substring(1)

or

function Random(N) { return Math.floor(N*(Math.random()%1)) }

function f() { var x = Random(16) ; return x + 48 + 7*(x>9) }

c = String.fromCharCode(f(),f(),f(),f(),f(),f())
Remember to add the "#" !

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #3

P: n/a
Dr John Stockton wrote on 19 sep 2004 in comp.lang.javascript:
You mean floor, not round;
Yes, did not see that.
and there is the Opera bug that means that
Math.random can give 1.0.
I prefer a musical for modus operandi.
The components can be concatenated in a single statement. But I'd use a
loop.


I knew you would say that, John, and you are right.

The question is if that would be as understandable to newbees.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.