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

02-12-2007, 07:04 PM
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.

02-12-2007, 08:35 PM
Are you sure the source file is in the correct directory? If the entire original script was in the <head> it should work fine.

02-12-2007, 09:03 PM
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>
<script type="text/javascript">
new showLocalTime("clock", "server-php", 600, "short")

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
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

var thisobj=this
setTimeout(function(){thisobj.updateTime()}, 1000) //update time every second

var thisobj=this
if (this.displayversion=="long")
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

02-12-2007, 10:10 PM
Unless your server routinely parses PHP code in .js files, you should call your external file:


and use:

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

There could also be other problems.

02-12-2007, 10:12 PM
.PHP? Are you sure John? A js file would create a bunch of parse errors in php... hmm.

02-12-2007, 10:40 PM
Yes, as long as the only PHP code in the script is:

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.

02-12-2007, 10:43 PM
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 :p

02-12-2007, 10:53 PM
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.

02-12-2007, 11:00 PM
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 :p Thanks John.

02-13-2007, 12:39 AM
Wow, thanks. You both rock. It works changing the .js to .php. Sweet.