Forum Moderators: open
I found over internet this script for my web site that displays the time.It works fine in mozilla but in IE it dont displays the time.
the code is:
<SCRIPT LANGUAGE="JavaScript">
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source! [javascript.internet.com...] -->
<!-- Begin
var timerID = null;
var timerRunning = false;
function stopclock (){
if(timerRunning)
clearTimeout(timerID);
timerRunning = false;
}
function showtime () {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds()
var timeValue = "" + ((hours >12) ? hours -12 :hours)
if (timeValue == "0") timeValue = 12;
timeValue += ((minutes < 10) ? ":0" : ":") + minutes
timeValue += ((seconds < 10) ? ":0" : ":") + seconds
timeValue += (hours >= 12) ? " P.M." : " A.M."
document.clock.face.value = timeValue;
timerID = setTimeout("showtime()",1000);
timerRunning = true;
}
function startclock() {
stopclock();
showtime();
}
// End -->
</SCRIPT>
also there is this script:
<BODY onLoad="startclock()">
<p><CENTER>
<FORM name="clock">
<input type="text" name="face" size=13 value="">
</FORM>
</CENTER>
<p><center>
</center><p>
Please help if this don't works can you suggest any other that displays the time.
Elio
Here is a version that allows for multiple clocks on a single page and doesn't use a lot of global variables. It uses document.getElementById instead of relying on a form with the name "clock" and an input named "face":
<html>
<head>
<title>Clocks</title>
</head>
<body>
<input type="text" id="clock1">
<input type="text" id="clock2">
<script type="text/javascript">
// Put this script just before your closing body tag
(function () {
/**
* A Clock which will update every second.
* @constructor
* @param id {String} The id of the input element that will display the time
* @param offset {Integer} An optional offset (in minutes). This can be used to
* display the time from a different timezone for example.
*/
function Clock(id, offset) {
this.el = document.getElementById(id);
this.timerId = null;
this.running = false;
this.offset = offset ¦¦ 0;
}
Clock.prototype = {
/**
* Start the clock running. If in invalid id was passed in the constructor
* then nothing will happen.
*/
startClock: function () {
if (this.el) {
this.showTime();
}
},
/**
* Stop the clock.
*/
stopClock: function () {
if (this.running) {
clearTimeout(this.timerId);
}
this.running = false;
},
/**
* Update the time value and display it in an input element. Note, a good
* update to this script would be to support other display methods, like
* putting the value in a div, not only using text input elements.
*/
showTime: function () {
var that = this;
var now = new Date((new Date()).getTime() + (this.offset * 1000 * 60)),
hours = now.getHours(),
minutes = now.getMinutes(),
seconds = now.getSeconds(),
timeValue = (hours > 12 ? hours - 12: hours);
if (timeValue == 0) {
timeValue = 12;
}
timeValue += (minutes < 10 ? ":0" : ":") + minutes;
timeValue += (seconds < 10 ? ":0" : ":") + seconds;
timeValue += (hours >= 12 ? " P.M." : " A.M.");
this.el.value = timeValue;
this.timerId = setTimeout(function () {that.showTime();}, 1000);
this.running = true;
}
};
var c1 = new Clock('clock1'),
c2 = new Clock('clock2', -60); // 1 hour ago
c1.startClock();
c2.startClock();
})();
</script>
</body>
</html>
i copied the script in dreamweaver but when i published there was no time display.
Now i am searching for a script that displays the date and and time can you suggest me one,becouse i have decided to have date and time both in one script.
I mean that both has to update automatically.
I dont understsand why some script works in Firefox,Chrome but they don't work in IE? i am new to this things can you explain it to me.
Thanks
Please note, the example I gave takes the id of an element the page, so if you are missing the correct ids then that would cause the script to not work.