Forum Moderators: open
This is my first post here so please be gentle with me. I discoved this forum via Google, and advice was offered to someone with a similar problem but I still can't get this routine to work with FF or Opera, although it works perfectly with IE... any suggestions would be greatly appreciated :O)
<body onLoad="clock()">
<SCRIPT LANGUAGE="JavaScript">
function clock() {
if (!document.layers && !document.all) return;
var digital = new Date();
var hours = digital.getHours();
var minutes = digital.getMinutes();
var seconds = digital.getSeconds();
var ampm = "am";
if (hours > 11) ampm = "pm";
if (hours > 12) hours = hours - 12;
if (hours == 0) hours = 12;
if (minutes <= 9) minutes = "0" + minutes;
if (seconds <= 9) seconds = "0" + seconds;
dispTime = hours + ":" + minutes + ampm + " ";
if (document.layers) {
document.layers.clktime.document.write(dispTime);
document.layers.clktime.document.close();
}
else
if (document.all)
document.getElementById('clktime').innerHTML=dispTime;
setTimeout("clock()", 1000);
}
[/script]
<span id="clktime"></span>
Try this:-
<script type="text/javascript">
function RunClock() {
var today = new Date();
var h = today.getHours();
var h1 = h;
var m = today.getMinutes();
var s = today.getSeconds();
var ampm = " AM";
if (h >= 12) {ampm = " PM"}
if (h >= 13) {h = h - 12}
if (h1 == 0) {h = 12}
h = leadZero(h); // if zero before hours required
m = leadZero(m);
s = leadZero(s);
document.getElementById('Clock').value = h + ":" + m + ":"+ s + ampm;
t = setTimeout('RunClock()',1000);
}
function leadZero(x) {
if (x < 10){x = "0" + x}
return x;
}
</script>
</head>
<body onload = "RunClock()">
<input type = "text" id = "Clock" name = "Clock">
Then; if you check for document.all (the old IE way of handling "named objects"), then don't use document.getElementById. Either check for the existence of document.getElementById and use document.getElementById('clktime'), or check document.all, and use document.all['clktime'], but do not mix them.
So with this in mind, you change your code as follows:
[pre]<script language="JavaScript" type="text/javascript">
function clock() {
if (!document.layers && !document.all && !document.getElementById) return;
var digital = new Date();
var hours = digital.getHours();
var minutes = digital.getMinutes();
var seconds = digital.getSeconds();
var ampm = "am";
if (hours > 11) ampm = "pm";
if (hours > 12) hours = hours - 12;
if (hours == 0) hours = 12;
if (minutes <= 9) minutes = "0" + minutes;
if (seconds <= 9) seconds = "0" + seconds;
dispTime = hours + ":" + minutes + ampm + " ";
[i]//
// Note that I changed the order of checking the different methods,
// as in 99% of cases it will work.[/i]
if (document.getElementById) {
document.getElementById('clktime').innerHTML = dispTime;
} else if (document.layers) {
document.layers.clktime.document.write(dispTime);
document.layers.clktime.document.close();
} else if (document.all)
document.all['clktime'].innerHTML = dispTime;
setTimeout("clock()", 1000);
}
</script>[/pre]