JavaScript: Random Colors for Text/Font, Background, and Border

Friday, June 6, 2008

In my search for a solution to make web pages look different and dynamic, I came up with an idea to make a part of the page's colors change to a random color each time the page is reloaded. I was limited in the way I could come up with a solution: I could not edit the actual HTML or other code of the web pages, I could only add to the page. Here's what I came up with...

I hope this code can help anyone out there looking to accomplish this. :o)

<Download> randomcolors.zip

Click here for a working example

Step 1:

Create a file named randomcolors.js in your favorite text editor and paste the following code into the file:

/*
* Java-Script Random Color Script
*
* Version 1.0
* Date: 6/4/2008
*
* Description: This script will pick a random color and set the desired
* text, background, or border to the random color
*
* Usage: 1. Add this Java-Script line to the head of your html page -
* <SCRIPT LANGUAGE="JavaScript" SRC="randomcolors.js"></SCRIPT>
*
* 2. Set the object's id tag to the corresponding names below to
* change to a random color
*
* Example: <div id="randomColor">This text will have a random color</div>
*
* randomColor - text color
* randomBgColor - background color
* randomBorderColor - border color
*
*/

var rColors = new Array() // DO NOT CHANGE THIS LINE

// COLORS TO RANDOMIZE:
// -Change these hex color codes to your desired colors to randomize
// -Add/remove as many colors to meet your needs

rColors[0] = '000099'
rColors[1] = '339900'
rColors[2] = 'FF0000'
rColors[3] = 'FFFF00'
rColors[4] = '0000CC'
rColors[5] = '33FF00'
rColors[6] = '6666FF'
rColors[7] = 'FF9900'
rColors[8] = 'CC0000'
rColors[9] = '0000FF'

// DO NOT CHANGE BELOW THIS LINE

// Adds a function to the onload event handler
function addOnLoadEvent(func) {
var oldOnLoad = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
// Run previous onload functions/events before running new function
window.onload = func(); {
if (oldOnLoad) {
oldOnLoad();
}
func();
}
}
}

// Choose a random font color
var whichColor = Math.round(Math.random()*(rColors.length-1));

// Choose a random background color
var whichBgColor = Math.round(Math.random()*(rColors.length-1));

// Choose a random border color
var whichBorderColor = Math.round(Math.random()*(rColors.length-1));

// Set random colors
function setRandomColors() {

// Set random font color
if(undefined != document.all.randomColor) {
document.getElementById("randomColor").style.color = rColors[whichColor];
}

// Set random background color
if(undefined != document.all.randomBgColor) {
document.getElementById("randomBgColor").style.backgroundColor = rColors[whichBgColor];
}

// Set random border color
if(undefined != document.all.randomBorderColor) {
document.getElementById("randomBorderColor").style.borderColor = rColors[whichBorderColor];
}
}

// Execute script
addOnLoadEvent(setRandomColors);

Step 2:

Create a file named randomcolors.html and paste the following code into the file:
<html>
<head>
<title>Random Color Script</title>
</head>
<body>
<SCRIPT LANGUAGE="JavaScript" SRC="randomcolors.js"></SCRIPT>
<div id="randomColor" align="center" style="padding: 10px; width: 200px; font-weight: bold;">Random Color Script</div>
<br />
<div id="randomBgColor" align="center" style="color: White; padding: 10px; width: 200px; font-weight: bold;">Random Color Script</div>
<br />
<div id="randomBorderColor" align="center" style="padding: 10px; width: 200px; border-width: 4px; border-style: solid;">Random Color Script</div>
</body>
</html>

Open randomcolors.html and viola! Each time you reload the page, you should see new colors for each object.

Issues you may encounter:

1. Make sure both files are in the same folder. Alternatively, you can change the JavaScript SRC tag in randomcolors.html to reflect the location of randomcolors.js.
2. If the script does not work and you are testing the script on your desktop or laptop, you may need to change the file extension of randomcolors.js to something else (like randomcolors.js_). There may be a restriction of executing a .js file on you computer.


The Right Brain vs Left Brain Test

I came across an interesting blog post which includes an image of a spinning dancer, which (supposedly) determines whether you are right or left brain oriented.

Do you see the dancer turning clockwise or counter-clockwise?




If clockwise, you use more of the right side of the brain.
If counter-clockwise, then you use more of the left side of the brain.



I see the dancer turning clockwise. I've tried to see the dancer turn the other way, but haven't been able to see the dancer turn counter-clockwise... I'll try to get my brain to see it turn counter-clockwise later....

How it works: