473,396 Members | 2,029 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,396 software developers and data experts.

random bgcolor

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
3 4996
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Terry Haufler | last post by:
I am trying to swap/rotate random flash banners using the following Javascript code. I have 3 flash headers/banners. I can get it to open a page with a random header using...
6
by: Tim Johnson | last post by:
Hello All: Using javascript to dynamically add row elements to a table. as in ..... row.setAttribute("bgcolor",rowColor); // or cell.setAttribute("bgcolor",rowColor); Using firefox or...
15
by: @sh | last post by:
Trying to do this, but its not working...?! My aim being to change the bgcolor of the cell upon which the user has rolled over, I'll then replicate this as an OnMouseOut to change it back. ...
6
by: acord | last post by:
Hi, I want to change the background color of a ul/li row, but the following code is not working as I expected. It works in <table><tr>... <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01...
12
by: GaryDean | last post by:
In the original post I failed so indicate that I am using framework 1.1....... I need to be able to change the background color of a page from code. I found an answer to this question in...
4
by: tshad | last post by:
I am trying to set up an Image authorization where you type in the value that is in a picture to log on to our site. I found a program that is supposed to do it, but it doesn't seem to work. ...
3
by: fran7 | last post by:
Hi, Anyone know how to adjust this so that the results appear in a random order? is it in the "Order By PostCardID DESC" part? ..asp <!--#Include File="art/dbconnect.asp"--> <% ...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.