homepage Welcome to WebmasterWorld Guest from 54.243.13.30
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Firefox problem with simple text clock!
Text Clock Problem
pb268




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

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>

 

Philip_M




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

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

pb268




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

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.

gergoe




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

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

pb268




msg:3595154
 7:14 pm on Mar 8, 2008 (gmt 0)

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)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved