View Full Version : Resolved Insufficient explanation given. Unable to call the code.

06-21-2010, 08:08 PM
I am learning javascript from this site, (http://www.yourhtmlsource.com/javascript/dateandtimescripts.html) and one of the programs code given is now explained to the point that I can not use it. I don't know how to call it. I would be glad if someone would examine the code to see how it should be applied.

Date and Time Scripts


Philip M
06-21-2010, 10:42 PM
This is ancient code. <script language=javascript> is long deprecated and obsolete. The <!-- and //--> comment (hiding) tags have not been necessary since IE3 (i.e. since September 1997). If you see these in some published script as here) it is a warning that you are looking at ancient and perhaps unreliable code. Any reference to Netscape ought to make it clear that the code is old and possibly unuseable.

There have been a good many excellent date/time scripts posted in this forum. Here is an example:-

<script type = "text/javascript">

function startTime() {
var now = new Date();
var h=now.getHours();
var min=now.getMinutes();
var s=now.getSeconds();
var ampm=(now.getHours()>11)?"PM":"AM";
var d=now.getDay();
var y=now.getFullYear();
var mon=now.getMonth();
var dm=now.getDate();
var endings=["st","nd","rd","th"];
var dayendings=[0,1,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,0,2,3,4,4,4,4,4,4,1];
var days=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var months=["January","February","March","April", "May", "June","July","August","September", "October", "Novemeber","Decemeber"];
if (h>12) {h-=12}
if (h==0) {h=12};
if (min<10) {min="0"+min}
if (s<10) {s="0"+s}
if (dm<10) {dm="0"+dm};
document.getElementById("time").innerHTML=h+":"+min+":"+s+" "+ampm+" "+d+" "+mon+" "+dm+" "+y;
var tim = setTimeout("startTime()",1000);

<body onload="startTime();">
<span id="time" style="color:#583B00;background-Color:yellow"></span>

If this does not meet your needs and you are unable to modify it yourself to suit, please come back.
Illiterate? Write today for free help. - Government leaflet.

06-22-2010, 08:37 AM
Thanks PhilipM,

My problem generally is how to call them on screen with HTML when I want to display them.
Document.write does not always produce what I want.

I really need a good on screen Javascript teaching program. I'm still looking.

Regards Frank

Philip M
06-22-2010, 09:08 AM
Document.write does not always produce what I want.

document.write statements must be run before the page finishes loading. Any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page. So document.write is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

You should use document.getElementById(), e.g.

<div id = "myDiv" style="display:none">
Content of div initially hidden

<script type = "text/javascript">
function showDiv() {
document.getElementById("myDiv").style.display="block"; // now shown

06-22-2010, 11:19 AM
Thank you Philip. It's all slowly getting into my old brain, or as 'Tesco' says.
"Every little helps" and boy, do I need help.