Forum Moderators: open

Message Too Old, No Replies

script works fine in mozilla but not in IE

         

Elio88

8:49 am on Nov 20, 2009 (gmt 0)

10+ Year Member



Hey

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

Fotiman

3:42 pm on Nov 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to WebmasterWorld! That script appears to be VERY old. The language attribute should not be used anymore, and the center element should not be used either. Also, that version uses global variables and allows for only 1 clock per page.

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>

Elio88

2:37 pm on Nov 22, 2009 (gmt 0)

10+ Year Member



thanks for the help

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

MartinWeb

5:09 pm on Nov 22, 2009 (gmt 0)

10+ Year Member



JS that works in Fireworks or Chrome, sometimes doesn't work in IE because the language is different in each browser. Because of that, you have to sometimes create different scripts for different browsers. I assume that this has something too do with the browser's engine... Does anyone know more?

Fotiman

3:20 pm on Nov 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Note, the example script I posted above works in Firefox, Chrome, IE, etc. Take the code example above, paste it in to notepad, save the file, and open it in your browser and you will see it work.

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.