Wednesday, 2 November 2011

BackGround Color change By using 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>Good Bye</H1>
</BODY>

No comments:

Post a Comment

Visual comparison of the two methods, creating a simple table.

Option 1, using PHP: // PHP $html = '<table>' ;     foreach ( $data as $row ) {     $html .= '<tr>...