Hey guys…
Although I am not a newbee I am not too knowledged in js and I code in it occasionally, so please excuse the question if it is steight forward…
Now…
Been working on an analogue clock script, customised it exactly to my needs, until I got to the point of calling it in my body…
I am trying to enter the clock inside a div but when I call it by document.getElementById nothing happens. Console on firefox says that it returns null. Initially I thought the error was inside the script, so to test it I called the clock by document.write(). But then it worked fine! It is not what I need it but it shows that the script works (right?)…
Please give me your advise on this as well as any other comments on the script…
I have combined js, styles and html in one file here:
[url]http://homepage.mac.com/p_koumoundouros/.public/analogClock.html
Thank you in advance!
<i>
</i><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AnalogClock</title>
<style type="text/css">
#clock {
position:relative;
margin:auto;
overflow:visible;
background: url(image.ext) no-repeat;
z-index:995; }
.numbers,.minutes,.hours,.seconds {visibility:hidden;position:absolute;top:0px;left:0px;}
.numbers {
padding-top:10px;
font-size:9px;
font-weight:bold;
text-align:center;
z-index:996; }
.minutes {
width:1px;
height:1px;
font-size:1px;
z-index:997; }
.hours {
width:2px;
height:2px;
font-size:2px;
z-index:998; }
.seconds {
width:1px;
height:1px;
font-size:1px;
z-index:999; }
</style>
<script type="text/javascript">
/*<![CDATA[*/
var diameter=400;
var numColor='666666';
var secColor='999999';
var colminutes='cccccc';
var hourColor='000000';
var x=0;
var y=0;
var pi=Math.PI;
var piHalf=pi/2;
var rad=parseInt(diameter) / 2;
var xCenter=parseInt(x) + rad;
var yCenter=parseInt(y) + rad;
var hourLength=1;
var minLength=2;
var secLength=2;
for (var i=0; i<(rad/2)+(rad/16); i++) {hourLength+=1;}
for (var i=0; i<(rad/2)-(rad/8); i++) {minLength+=2; secLength+=2;}
var offset=16;
var numbers=[,1,2,3,4,5,6,7,8,9,10,11,12];
var clock;
function formatTime() {
var now=new Date();
var second = now.getSeconds();
var minute = now.getMinutes();
var hour = now.getHours();
if (hour > 11) {hour -= 12;} else hour = hour;
return [hour,minute,second];
}
function updateClock() {
var time = formatTime();
var secondHand = pi * time[2] / 30 - piHalf;
var minuteHand = pi * time[1] / 30 - piHalf;
var hourHand = pi * time[0] / 6 + pi * parseInt(time[1])/360 - piHalf;
for (var i = 0; i < secLength; i++) {document.getElementById('seconds' + i).style.top = (yCenter + i * Math.sin(secondHand))+'px'; document.getElementById('seconds' + i).style.left = (xCenter + i * Math.cos(secondHand))+'px'; document.getElementById('seconds' + i).style.visibility = 'visible';}
for (var i = 0; i < minLength; i++) {document.getElementById('minutes' + i).style.top = (yCenter + i * Math.sin(minuteHand))+'px'; document.getElementById('minutes' + i).style.left = (xCenter + i * Math.cos(minuteHand))+'px'; document.getElementById('minutes' + i).style.visibility = 'visible';}
for (var i = 0; i < hourLength; i++) {document.getElementById('hours' + i).style.top = (yCenter + i * Math.sin(hourHand))+'px'; document.getElementById('hours' + i).style.left = (xCenter + i * Math.cos(hourHand))+'px'; document.getElementById('hours' + i).style.visibility = 'visible';} setTimeout('updateClock()', 100);
}
var clockDIVopen='<div id="clock" style="width:'+(diameter+offset*2)+'px;height:'+(diameter+offset*2)+'px;">'; clock=clockDIVopen;
for (var i=1; i<13; i++) {
var cnumDIV='<div id="numbers'+i+'" style="width:'+(offset*2)+'px;height:'+(offset*2)+'px;color:#'+numColor+';" class="numbers">'+numbers[i]+'</div>'; clock+=cnumDIV; }
for (var i=0; i<minLength; i++) {
var cminDIV='<div id="minutes'+i+'" style="background:#' + colminutes + ';" class="minutes"></div>'; clock+=cminDIV; }
for (var i=0; i<hourLength; i++) {
var chourDIV='<div id="hours'+i+'" style="background:#'+hourColor+';" class="hours"></div>'; clock+=chourDIV; }
for (var i=0; i<secLength; i++) {
var csecDIV='<div id="seconds'+i+'" style="background:#'+secColor+';" class="seconds"></div>'; clock+=csecDIV; }
var clockDIVclose='</div>'; clock+=clockDIVclose;
function displayClock() {
if (!document.getElementById) return;
for (var i = 1; i < 13; i++) {
document.getElementById('numbers' + i).style.top = (yCenter - offset + rad * Math.sin(i * pi / 6 - piHalf))+'px';
document.getElementById('numbers' + i).style.left = (xCenter - offset + rad * Math.cos(i * pi / 6 - piHalf))+'px';
document.getElementById('numbers' + i).style.visibility = 'visible';
}
updateClock();
}
window.onload = function(){
document.getElementById('theClock').innerHTML=clock;
displayClock();
}
/*]]>*/
</script>
</head>
<body>
<div id="theClock"></div>
</body>
</html>
function displayClock() {
if (!document.getElementById) return;
for (var i = 1; i < 13; i++) {
document.getElementById('numbers' + i).style.top = (yCenter - offset + rad * Math.sin(i * pi / 6 - piHalf))+'px';
document.getElementById('numbers' + i).style.left = (xCenter - offset + rad * Math.cos(i * pi / 6 - piHalf))+'px';
document.getElementById('numbers' + i).style.visibility = 'visible';
}
updateClock();
}
document.write(clock);
document.getElementById('theClock').innerHTML=clock;
window.onload = displayClock;
[/quote]
function displayClock() {
if (!document.getElementById) return;
document.getElementById('theClock').innerHTML=clock;
for (var i = 1; i < 13; i++) {
document.getElementById('numbers' + i).style.top = (yCenter - offset + rad * Math.sin(i * pi / 6 - piHalf))+'px';
document.getElementById('numbers' + i).style.left = (xCenter - offset + rad * Math.cos(i * pi / 6 - piHalf))+'px';
document.getElementById('numbers' + i).style.visibility = 'visible';
}
updateClock();
}
window.onload = displayClock;
[code=html]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>analogClockComplete</title>
<style type="text/css">
@charset "utf-8";
#analogClock{}
#theClock {
position:relative;
margin:0;
overflow:visible;
background: url(image.ext) no-repeat;
z-index:995; }
.numbers,.minutes,.hours,.seconds {position:absolute;top:0px;left:0px;}
.numbers {
padding-top:10px;
font-size:9px;
font-weight:bold;
text-align:center;
z-index:996; }
.minutes {
width:1px;
height:1px;
font-size:1px;
z-index:997; }
.hours {
width:2px;
height:2px;
font-size:2px;
z-index:998; }
.seconds {
width:1px;
height:1px;
font-size:1px;
z-index:999; }
</style>
<script type="text/javascript">
/*<![CDATA[*/
var diameter=200;
var numColor='666666';
var secColor='999999';
var minColor='cccccc';
var hourColor='000000';
var x=0;
var y=0;
var pi=Math.PI;
var piHalf=pi/2;
var rad=parseInt(diameter) / 2;
var xCenter=parseInt(x) + rad;
var yCenter=parseInt(y) + rad;
var hourLength=1;
var minLength=2;
var secLength=2;
for (var i=0; i<(rad/2)+(rad/16); i++) {hourLength+=1;}
for (var i=0; i<(rad/2)-(rad/8); i++) {minLength+=2; secLength+=2;}
var offset=16;
var numbers=[,1,2,3,4,5,6,7,8,9,10,11,12];
function formatTime() {
var now=new Date();
var second=now.getSeconds();
var minute=now.getMinutes();
var hour=now.getHours();
if (hour>11) {hour-=12;} else hour=hour;
return [hour,minute,second];
}
function updateClock() {
var time=formatTime();
var secondHand=pi*time[2]/30-piHalf;
var minuteHand=pi*time[1]/30-piHalf;
var hourHand=pi*time[0]/6+pi*parseInt(time[1])/360-piHalf;
for (var i=0; i<secLength; i++) {
document.getElementById('seconds'+i).style.top=(yCenter+i*Math.sin(secondHand))+'px';
document.getElementById('seconds'+i).style.left=(xCenter+i*Math.cos(secondHand))+'px';
}
for (var i=0; i<minLength; i++) {
document.getElementById('minutes'+i).style.top=(yCenter+i*Math.sin(minuteHand))+'px';
document.getElementById('minutes'+i).style.left=(xCenter+i*Math.cos(minuteHand))+'px';
}
for (var i=0; i<hourLength; i++) {
document.getElementById('hours'+i).style.top=(yCenter+i*Math.sin(hourHand))+'px';
document.getElementById('hours'+i).style.left=(xCenter+i*Math.cos(hourHand))+'px';
}
setTimeout(updateClock, 100);
}
var clockDIVopen='<div id="theClock" style="width:'+(diameter+offset*2)+'px;height:'+(diameter+offset*2)+'px;">';
var clock=clockDIVopen;
for (var i=1; i<13; i++) {
var cnumDIV='<div id="numbers'+i+'" style="width:'+(offset*2)+'px;height:'+(offset*2)+'px;color:#'+numColor+';" class="numbers">'+numbers[i]+'</div>';
clock+=cnumDIV; }
for (var i=0; i<minLength; i++) {
var cminDIV='<div id="minutes'+i+'" style="background:#'+minColor+';" class="minutes"></div>';
clock+=cminDIV; }
for (var i=0; i<hourLength; i++) {
var chourDIV='<div id="hours'+i+'" style="background:#'+hourColor+';" class="hours"></div>';
clock+=chourDIV; }
for (var i=0; i<secLength; i++) {
var csecDIV='<div id="seconds'+i+'" style="background:#'+secColor+';" class="seconds"></div>';
clock+=csecDIV; }
var clockDIVclose='</div>';
clock+=clockDIVclose;
function analogClock() {
if (!document.getElementById) return;
document.getElementById('analogClock').innerHTML=clock;
for (var i=1; i<13; i++) {
document.getElementById('numbers'+i).style.top=(yCenter-offset+rad*Math.sin(i*pi/6-piHalf))+'px';
document.getElementById('numbers'+i).style.left=(xCenter-offset+rad*Math.cos(i*pi/6-piHalf))+'px';
}
updateClock();
}
function toggle(id) {
var e = document.getElementById(id);
if(e.style.visibility == 'visible' || !e.style.visibility) {e.style.visibility = 'hidden';}
else {e.style.visibility = 'visible';}
return false;
}
/*]]>*/
</script>
</head>
<body onload="analogClock();">
<div id="placeholder"><div id="analogClock">analogClock</div></div>
<a href="#" onclick="return toggle('placeholder');">Toggle</a>
</body>
</html>[/code]
0.1.9 — BETA 5.18