#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    194
    Rep Power
    76

    Having trouble with a javascript script


    javascript Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Javascript Power</title> 
    <script type="text/javascript"> 
    function leadZero(x) { 
                if (x < 10){x = "0" + x;}
                    return x; 
            }
             function runClock() { 
                var today = new Date();
     
                var h = today.getHours();
                var h1 = h; 
                var m = today.getMinutes();
                var s = today.getSeconds(); 
                var ampm = " AM";
     
                if (h >= 12) {ampm = " PM";}
                if (h >= 13) {h = h - 12;}
                if (h1 === 0) {h = 12;}
     
                h = leadZero(h);  // if zero before hours is required
                m = leadZero(m); 
                s = leadZero(s); 
     
                document.getElementById('Clock').innerHTML = "Current Time <br />" + h + ":" +             m + ":"+ s + ampm; // delete ampm if desired
     
            } 
            function startTime() {
                runClock();    // to start right away
                setInterval(runClock,1000);
            }
            function CallDiv() {
                var divTag = document.createElement("div");
                divTag.id = "Clock";
                divTag.style.cssText = "display:block;font-family:Arial,Helvetica,sans-serif;font-weight:bold;font-size:10pt;line-height:100%; text-align: center;padding-top:10px;";
                document.body.appendChild(divTag);
                startTime();
             }
     
    window.onload = CallDiv(); 
    </script>
    </head>
    <body>  
    </body>
    </html>


    for some reason my script isn't working even though the syntax is correct

    and for some reason it works on my jsfiddle but not on a live page

    http://jsfiddle.net/jack13580/ZdXCP/

    and live page http://the-test.comoj.com/files/javascript-power.html
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,969
    Rep Power
    374
    when i run this code in my firefox it reports "document.body is null"

    document.body.appendChild(divTag); maybe you cannot append if it is empty? however i put in randome text and that didnt work!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0
    You're invoking callDiv, instead of assigning it to the onload event.

    window.onload = CallDiv() should be window.onload = CallDiv;

    Comments on this post

    • Kravvitz agrees
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    194
    Rep Power
    76
    wow I new it was a dumb mistake I made thanks

IMN logo majestic logo threadwatch logo seochat tools logo