Results 1 to 10 of 10

Thread: script: DD Local Time, can't link to js...can only embed in page

  1. #1
    Join Date
    Nov 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default script: DD Local Time, can't link to js...can only embed in page

    1) Script Title: Local Time

    2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex6/localtime.htm

    3) Describe problem:
    I want to link to the script in the head, instead of including the entire script in the head. When I enter the entire script into an external .js file, then link to it as follows:

    <script src="/source/scripts/clock.js" type="text/javascript"></script>

    the script no longer works.

  2. #2
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Are you sure the source file is in the correct directory? If the entire original script was in the <head> it should work fine.
    - Mike

  3. #3
    Join Date
    Nov 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks Mike. Yes, I'm sure the path is correct. The clock appears to work, but it always shows the same time upon page refresh: 7:13pm. The site is here: http://tinyurl.com/2zbuqh (Clock in footer.)

    When embedded in the page, everything works fine.

    In the page I have the following code:
    <script src="/source/scripts/clock.js" type="text/javascript"></script>
    and
    <script type="text/javascript">
    new showLocalTime("clock", "server-php", 600, "short")
    </script>

    Then in the linked .js file, I have the whole script. I am pasting the exact contents of the script below:

    /***********************************************
    * Local Time script- &#169; Dynamic Drive (http://www.dynamicdrive.com)
    * This notice MUST stay intact for legal use
    * Visit http://www.dynamicdrive.com/ for this script and 100s more.
    ***********************************************/

    var weekdaystxt=["Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"]

    function showLocalTime(container, servermode, offsetMinutes, displayversion){
    if (!document.getElementById || !document.getElementById(container)) return
    this.container=document.getElementById(container)
    this.displayversion=displayversion
    var servertimestring=(servermode=="server-php")? '<? print date("F d, Y H:i:s", time())?>' : (servermode=="server-ssi")? '<!--#config timefmt="&#37;B %d, %Y %H:%M:%S"--><!--#echo var="DATE_LOCAL" -->' : '<%= Now() %>'
    this.localtime=this.serverdate=new Date(servertimestring)
    this.localtime.setTime(this.serverdate.getTime()+offsetMinutes*60*1000) //add user offset to server time
    this.updateTime()
    this.updateContainer()
    }

    showLocalTime.prototype.updateTime=function(){
    var thisobj=this
    this.localtime.setSeconds(this.localtime.getSeconds()+1)
    setTimeout(function(){thisobj.updateTime()}, 1000) //update time every second
    }

    showLocalTime.prototype.updateContainer=function(){
    var thisobj=this
    if (this.displayversion=="long")
    this.container.innerHTML=this.localtime.toLocaleString()
    else{
    var hour=this.localtime.getHours()
    var minutes=this.localtime.getMinutes()
    var seconds=this.localtime.getSeconds()
    var ampm=(hour>=12)? "PM" : "AM"
    var dayofweek=weekdaystxt[this.localtime.getDay()]
    this.container.innerHTML=formatField(hour, 1)+":"+formatField(minutes)+":"+formatField(seconds)+" "+ampm+" ("+dayofweek+")"
    }
    setTimeout(function(){thisobj.updateContainer()}, 1000) //update container every second
    }

    function formatField(num, isHour){
    if (typeof isHour!="undefined"){ //if this is the hour field
    var hour=(num>12)? num-12 : num
    return (hour==0)? 12 : hour
    }
    return (num<=9)? "0"+num : num//if this is minute or sec field
    }

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Unless your server routinely parses PHP code in .js files, you should call your external file:

    clock.php

    and use:

    Code:
    <script src="/source/scripts/clock.php" type="text/javascript"></script>
    There could also be other problems.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    .PHP? Are you sure John? A js file would create a bunch of parse errors in php... hmm.
    - Mike

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Yes, as long as the only PHP code in the script is:

    Code:
    var servertimestring=(servermode=="server-php")? '<? print date("F d, Y H:i:s", time())?>' : (servermode=="server-ssi")? '<!--#config timefmt="%B %d, %Y %H:%M:%S"--><!--#echo
    The rest will be served 'as is' and since the type is set in the call as type="text/javascript", the browser will parse it all as javascript, including the data from the server which should replace the PHP tokens highlighted above.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    But javascript parses php as php. php however, doesn't for javascript. I'm not sure how that will work as a php file. I'm probably wrong, but I guess you learn something new every day
    - Mike

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The javascript parser is a part of the browser. It will parse whatever code the server serves it. The server will not parse anything as PHP unless told to do so. How this is done (telling the server to parse PHP) varies from server to server. This script relies upon the server recognising the shorthand:

    <? ?>

    convention for PHP.

    This (what code the server will parse) can be changed, at least in asp with the runat="server" for javascript and other languages but, for the most part, the server parses the server-side code and serves it along with the rest of the content to the browser which parses it all as whatever it is told that it is.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Ah, so if you run a .php file as a javascript file the browser will still parse the php output. I didn't know that, now I do Thanks John.
    - Mike

  10. #10
    Join Date
    Nov 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Wow, thanks. You both rock. It works changing the .js to .php. Sweet.

Bookmarks

Posting Permissions

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