Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder effpeetee's Avatar
    Join Date
    Feb 2007
    Location
    Clapham Junction - London SW
    Posts
    4,885
    Thanks
    228
    Thanked 204 Times in 203 Posts

    Exclamation Insufficient explanation given. Unable to call the code.

    I am learning javascript from this site, 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

    Frank
    Last edited by effpeetee; 06-22-2010 at 12:00 PM.
    * Sources (updated: 21.11.2012.
    Using Windows 8 Professional. 64bit with HP Photosmart 5510 printer Very useful site here.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    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:-

    Code:
    <html>
    <head>
    <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}
    dm+=endings[dayendings[dm]];
    if (dm<10) {dm="0"+dm};
    d=days[d];
    mon=months[mon-1];
    document.getElementById("time").innerHTML=h+":"+min+":"+s+" "+ampm+" "+d+" "+mon+" "+dm+" "+y;
    var tim = setTimeout("startTime()",1000);
    }
    
    </script>
    </head>
    <body onload="startTime();">
    <span id="time" style="color:#583B00;background-Color:yellow"></span>
    </body>
    </html>
    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.
    Last edited by Philip M; 06-21-2010 at 10:48 PM.

  • Users who have thanked Philip M for this post:

    effpeetee (06-22-2010)

  • #3
    Senior Coder effpeetee's Avatar
    Join Date
    Feb 2007
    Location
    Clapham Junction - London SW
    Posts
    4,885
    Thanks
    228
    Thanked 204 Times in 203 Posts
    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
    Last edited by effpeetee; 06-22-2010 at 10:17 AM.
    * Sources (updated: 21.11.2012.
    Using Windows 8 Professional. 64bit with HP Photosmart 5510 printer Very useful site here.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Quote Originally Posted by effpeetee View Post
    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.

    Code:
    <div id = "myDiv" style="display:none">
    Content of div initially hidden
    </div>
    
    
    <script type = "text/javascript">
    function showDiv() {
    document.getElementById("myDiv").style.display="block";  // now shown
    }
    </script>

  • #5
    Senior Coder effpeetee's Avatar
    Join Date
    Feb 2007
    Location
    Clapham Junction - London SW
    Posts
    4,885
    Thanks
    228
    Thanked 204 Times in 203 Posts
    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.

    Frank
    * Sources (updated: 21.11.2012.
    Using Windows 8 Professional. 64bit with HP Photosmart 5510 printer Very useful site here.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •