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.