Home Tutorial Javascript JavaScript change background color automatically

 
 

Share on Google+Share on Google+
JavaScript change background color automatically
Posted on: August 12, 2010 at 12:00 AM
Advertisement
In this section, you will learn how to change the background color of the web page automatically.

JavaScript change background color automatically

In this section, you will learn how to change the background color of the web page automatically. Now to implement this, we have defined 9 different colors and to display them randomly, we have used using the Math.random(). The window.setTimeout() function change the background color after every 5 seconds.

Here is the code:

<SCRIPT LANGUAGE="JavaScript">
function
setbackground(){
window.setTimeout( "setbackground()", 5000);
var index = Math.round(Math.random() * 9);
var ColorValue = "#E6A9EC";
if(index == 1)
ColorValue = "FFCCCC";
if(index == 2)
ColorValue = "CCAFFF";
if(index == 3)
ColorValue = "A6BEFF";
if(index == 4)
ColorValue = "99FFFF";
if(index == 5)
ColorValue = "D5CCBB";
if(index == 6)
ColorValue = "99FF99";
if(index == 7)
ColorValue = "FFFF99";
if(index == 8)
ColorValue = "FFCC99";
if(index == 9)
ColorValue = "CCCCCC";
document.bgColor=ColorValue;
}
</SCRIPT>
<
BODY onLoad="setbackground();">
<
H1>Hello World</H1>
</
BODY>

Output:

After every 5 seconds background color will get changed :

Advertisement

Related Tags for JavaScript change background color automatically:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 12, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com