684425 0 Posted December 22, 2014 Code works when... <script type="text/javascript"> var currenttime = '<?php print gmdate('F d, Y H:i:s', time() + (1 * 60 * 60))?>' var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December") var serverdate=new Date(currenttime) function padlength(what){ return (what.toString().length==1)? "0"+what : what } 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) } </script> Code not works when... <script type="text/javascript" src="clock.js"> I want to use second option. Any help? Quote Share this post Link to post Share on other sites
Monkuar 14 Posted December 22, 2014 (edited) Are you inserting that code at the exact same place? Sometimes the JS needs to be rendered before the ID. Is your clock.js in the same directory? Is it being called? If your on chrome go to dev tools and refresh and see if it's getting called. Edit: Oh I see, it's because you're using php for inline variables. You need to pass them through a parameter function instead if you want to use that js externally. Edited December 22, 2014 by Monkuar Quote Share this post Link to post Share on other sites
684425 0 Posted December 22, 2014 Edit: Oh I see, it's because you're using php for inline variables. You need to pass them through a parameter function instead if you want to use that js externally. Any example? Quote Share this post Link to post Share on other sites
CroNiX 58 Posted December 22, 2014 <script type="text/javascript" src="clock.js"> That assumes "clock.js" is in the same directory as the actual script being executed, which is probably not the case. Use a full path to clock.js, or a URL, like src="/assets/js/clock.js" or "http://yoursite.com/assets/js/clock.js". Quote Share this post Link to post Share on other sites
684425 0 Posted December 24, 2014 <script type="text/javascript" src="clock.js">That assumes "clock.js" is in the same directory as the actual script being executed, which is probably not the case. Use a full path to clock.js, or a URL, like src="/assets/js/clock.js" or "http://yoursite.com/assets/js/clock.js". <script type="text/javascript" src="clock.js"> <script type="text/javascript" src="tip.js"> both these files are in same directory along with index.php tip.jis is working fine but clock.js don't work. You said "you're using php for inline variables. You need to pass them through a parameter function instead" how can i do this? Quote Share this post Link to post Share on other sites
CroNiX 58 Posted December 24, 2014 For one thing, their JS files (with a js extension), so php won't parse them (unless you set your server up to parse js as php)...meaning you can't have php directly in the js file. Quote Share this post Link to post Share on other sites