Jump to content
684425

javascript attached file not works

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites
<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".

Share this post


Link to post
Share on other sites
<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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.