0

so please take into account I am a newbie. I currently use the following script throughout my pages to display the time:

var currenttime = '<? print date("F d, Y H:i:s", time())?>' //PHP method of getting server date
    var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December")
    var serverdate=new Date(currenttime)

    function padlength(what){
    var output=(what.toString().length==1)? "0"+what : what
    return output
    }

    function displaytime(){
    serverdate.setSeconds(serverdate.getSeconds()+1)
    var datestring=montharray[serverdate.getMonth()]+" "+padlength(serverdate.getDate())+", "+serverdate.getFullYear()
    var timestring=padlength(serverdate.getHours())+":"+padlength(serverdate.getMinutes())+":"+padlength(serverdate.getSeconds())
    document.getElementById("servertime").innerHTML=datestring+" "+timestring
    }

    window.onload=function(){
    setInterval("displaytime()", 1000)
    }

and display it with the following html:

<div id="header"><span class="time" id="servertime"></span></div>

what I assumed I could do, rather than having the same script in <head> of every page, put the script in an external .js file and include it using:

<script type="text/javascript" src="live_clock.js"></script>

now my root is .../ece70141/

I have just put it in the same directory (.js file as the others) just to get it to work. However its not working. Could somebody please advise me how to do this properly?

many thanks,

1
  • can I just include a php with the js in it? Commented May 31, 2011 at 9:58

2 Answers 2

1

You have to serve the JavaScript file through PHP otherwise <? print date("F d, Y H:i:s", time())?> will not be parsed.

It might be as simple as setting .php as file suffix (instead of .js) and adding

<?php 
    header('Content-type: text/javascript');
    // or  header('Content-type: application/javascript');
?>

at the top.

Sign up to request clarification or add additional context in comments.

5 Comments

i had done that already and it works, but the w3c validator didn't like it so I was trying to do it this way as I thought it was the valid method :S
at the top of where, sorry to be a newb.
@buy: At the top of the file. What did the w3c validator say?
i put the script in a global .php source i use (functions.php) and used the include within the <head> section. thank you for help.
@buy: If you include the script normally with <script type="text/javascript" src="live_clock.php"></script> then it will work fine. The header is only set for the content that is returned for this one request.
0

I'd say you should do something like this:

<script>
     var currenttime = '<?php echo date("F d, Y H:i:s", time()); ?>'
</script>
<script src="/your/path/to/the/javascript.js"></script>

1 Comment

and, of course, the javascript.js should not include that first line, where you set the currenttime.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.