PDA

View Full Version : javascript error


IKLOP
02-07-2003, 08:27 PM
I've been working on a clock that is initialized by the server time, then uses javascript to increase the time. I got it working, but I get errors. When I use IE 5.5 it says "Done, but with error's on the page" and it doesn't display the clock at all in Mozilla. Here's the code I use:


<script language='JavaScript' type='text/javascript'>
<!--

var clientdate = new Date();
var serverdate = new Date(<?php echo date("Y,m,d,H,i,s");?>);

var hourdif = clientdate.getHours() - serverdate.getHours();
serverdate.setHours( serverdate.getHours() + hourdif );
serverdate.setSeconds( serverdate.GetSeconds() - 1000 );

function clock()
{
if (!document.layers && !document.all)
{
return;
}

setTimeout("clock();",1000);
serverdate.setTime(serverdate.getTime()+1000);

var year = serverdate.getYear();
var month = serverdate.getMonth();
var day = serverdate.getDate();

var hours = serverdate.getHours();
var minutes = serverdate.getMinutes();
var seconds = serverdate.getSeconds();

if (day < 10) day = "0" + day;
if (month < 10) month = "0" + month;

if (hours > 11) amOrPm = "PM";
if (hours > 12) hours = hours - 12;
if (hours == 0) hours = 12;
if (minutes <= 9) minutes = "0" + minutes;
if (seconds <= 9) seconds = "0" + seconds;

dispDate = year + "." + month + "." + day + " - " + hours + ":" + minutes + ":" + seconds + " " + amOrPm;
if (document.layers)
{
document.layers.timer.document.write(dispDate);
document.layers.timer.document.close();
}
else
{
timer.innerHTML = dispDate;
}
}

-->
</script>

I then have a span tag with name='timer' which is where the time is displayed. If you want, you can see it in the link in my sig.

All help is greatly appreciated.
Thanks in advanced.

jeremy
02-08-2003, 08:45 AM
what model of mozilla are you using? mozilla sometimes doesnt accept some dhtml and javascript... that could be your problem...but i'm pretty sure accepts most of whats out there.

Jon Hanlon
02-09-2003, 05:42 PM
It won't work in Mozilla as Mozilla has neither document.layers nor document.all.
[To get it to work in Mozilla remove the layers && all test and add the line:
var timer = (document.getElementById) ? document.getElementById("timer") : document.all("timer")
before:
timer.innerHTML = dispDate;
]

It won't work in IE because:
there is no GetSeconds() method - use getSeconds()
amOrPm is not always defined. Add the line:
var amOrPm = "AM";
before the test.

Also, the getMonth() method returns 0..11, not 1..12 as you'd expect, so you should change it to read:
var month = serverdate.getMonth() + 1;

IKLOP
02-11-2003, 03:31 PM
Thanks :)
Two problems though:
1 - In Mozilla, the year is displayed as 103 but it works fine in IE.
2 - In Mozilla, everytime the second changes, the time flashes. I noticed this on another site also, so it may be unfixable?
Also, the getMonth() method returns 0..11, not 1..12 as you'd expect, so you should change it to read:
var month = serverdate.getMonth() + 1;I had originally done that but for some reason, I'm guessing the way I'm sending over the date using php, it gives the month from 1-12.

Thanks for the help,
-IKLOP

Jon Hanlon
02-11-2003, 06:18 PM
The getYear() method has been supplanted by getFullYear(), but unfortunately Netscape 4 knows nothing of it.
So either change it to getFullYear(), or say if (year < 1900) year += 1900;

To get over the screen flicker, try doing:
setTimeout("timer.innerHTML = dispDate",0)
or the like.