Welcome to WebmasterWorld Guest from 54.227.214.52

Forum Moderators: open

Firefox problem with simple text clock!

Text Clock Problem

   
3:26 pm on Mar 8, 2008 (gmt 0)

5+ Year Member



Hi Guys,

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>

4:24 pm on Mar 8, 2008 (gmt 0)

10+ Year Member



The script you have posted is very old.

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

4:35 pm on Mar 8, 2008 (gmt 0)

5+ Year Member



Huge thanks Philip_M

That code works perfectly... but... I would like the time displayed as normal text, rather than in a text box :O)

P.s.
My HTML and JavaScript knowledge is limited to the odds bits of code I have been able find here and there.

4:36 pm on Mar 8, 2008 (gmt 0)

10+ Year Member



<script language="JavaScript"> is not valid anymore (it has been removed from HTML 4.0), use <script type="text/javascript">, or combine the two.

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]
7:14 pm on Mar 8, 2008 (gmt 0)

5+ Year Member



Huge thanks gergoe :O)

That code works perfectly, and it's exactly what I'm looking for :O)

(I was going round in circles for ages)

 

Featured Threads

Hot Threads This Week

Hot Threads This Month