Thursday 2 June 2011

Automatic color changing Background JavaScript


Automatic color changing Background JavaScript

<html> 
<head> 
<title>JavaScript Experiments: Color Cycle in 51 Bytes</title> 
</head> 
<body id="x" onload="i=setInterval('x.bgColor=i++',9)"> 
</body>
</html>

Save the above JavaScript code in notepad with .html extension. Open that file with browser and  see the magic. The background colors are changing Automaticlly. 

* If you execute the below script the color will change for every 5 seconds. 

<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>



After every 5 seconds background color will get changed :

0 comments:

Post a Comment