684425 Posted December 22, 2014 Share 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 Link to comment https://forums.phpfreaks.com/topic/293222-javascript-attached-file-not-works/ Share on other sites More sharing options...
Monkuar Posted December 22, 2014 Share 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 Link to comment https://forums.phpfreaks.com/topic/293222-javascript-attached-file-not-works/#findComment-1500322 Share on other sites More sharing options...
684425 Posted December 22, 2014 Author Share 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 Link to comment https://forums.phpfreaks.com/topic/293222-javascript-attached-file-not-works/#findComment-1500343 Share on other sites More sharing options...
CroNiX Posted December 22, 2014 Share 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 Link to comment https://forums.phpfreaks.com/topic/293222-javascript-attached-file-not-works/#findComment-1500392 Share on other sites More sharing options...
684425 Posted December 24, 2014 Author Share 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 Link to comment https://forums.phpfreaks.com/topic/293222-javascript-attached-file-not-works/#findComment-1500549 Share on other sites More sharing options...
CroNiX Posted December 24, 2014 Share 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 Link to comment https://forums.phpfreaks.com/topic/293222-javascript-attached-file-not-works/#findComment-1500567 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.