SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,068
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)

    Question External Javascript Clock Not Working

    I found a Javascript clock (I intend to re-write and customise it to suit but want to just get something working to start) that works fine when embedded but I can't get to work properly when linked as an external file.

    The code for the clock division:
    HTML Code:
    	<div class="sys_clock">
    	    <script type="text/javascript" src="http://localhost/Filelocationhiddenfilelinksok"></script>
    <p><b>Current Server Time:</b> <span id="displaytime"></span></p>
    
    <p style="font: normal 11px Arial">This free script provided by<br />
    <a href="http://www.javascriptkit.com">JavaScript Kit</a></p>	    
    	    </script>
    		<noscript>
    			<?php echo date("H:i:s", time()) ?> UTC
    		</noscript>
    	</div>
    The java script:
    Code:
    // Current Server Time script (SSI or PHP)- By JavaScriptKit.com (http://www.javascriptkit.com)
    // For this and over 400+ free scripts, visit JavaScript Kit- http://www.javascriptkit.com/
    // This notice must stay intact for use.
    
    //Depending on whether your page supports SSI (.shtml) or PHP (.php), UNCOMMENT the line below your page supports and COMMENT the one it does not:
    //Default is that SSI method is uncommented, and PHP is commented:
    
    //var currenttime = '<!--#config timefmt="%B %d, %Y %H:%M:%S"--><!--#echo var="DATE_LOCAL" -->' //SSI method of getting server date
    var currenttime = '<? print date("F d, Y H:i:s", time())?>' //PHP method of getting server date
    
    ///////////Stop editting here/////////////////////////////////
    
    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)
    }
    It has run perfect when embedded, my guess is that I've made a simple beginners error somewhere as it stands it shows (in the browser) "Current Server Time:

    This free script provided by
    JavaScript Kit"

    So i know that it is at least partly working. Once I get the clock working and I've re-written the script I hope to have it so I can pass a few variables to the script and have it display the time and/or date in a certain way. Can the same external script be called as many times are required in the same page?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    The script requires PHP to parse it.
    <? print date("F d, Y H:i:s", time())?>

    You can use a date format extension, so that javascript can return dates in a similar manner.
    http://blog.stevenlevithan.com/archi...te-time-format

    After you've included the date format code, you can use it as follows

    Code javascript:
    var now = new Date();
    var currenttime = now.format('F d, Y H:i:s'); // javascript date format extension
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,068
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    The script requires PHP to parse it.
    <? print date("F d, Y H:i:s", time())?>

    You can use a date format extension, so that javascript can return dates in a similar manner.
    http://blog.stevenlevithan.com/archi...te-time-format

    After you've included the date format code, you can use it as follows

    Code javascript:
    var now = new Date();
    var currenttime = now.format('F d, Y H:i:s'); // javascript date format extension
    Would that always give the time on the server? The idea being that the clock time displayed will be the server time, which will be the same for any user.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Do you want the server time to be shown to someone from Dubai or Australia, or do you want the visitor to see a time that akes sense to them?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,068
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    It will be an on-line game with the displayed game time being UTC for all players. There will also be a current "tick" display. Players will have the option of using either for the coordination of their activities in the game. The completion time for anything being "built" will always be displayed as UTC.

    Once I've got a clock with the server time displayed, I might add an option for players to set their GMT (UTC) offset and have the on-screen clocks display their local time.

    Every BB-MMORPG that I've ever played has displayed the time on the clock as the time on the server on which the game runs.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Then the time shouldn't come from javascript, but from the server instead.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,068
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    That's where this comes in:

    var currenttime = '<? print date("F d, Y H:i:s", time())?>'

    The game will be coded mainly in php.

  8. #8
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, are you using ASP .Net 2.0 ? Are you dealing with a web user control or web page ?

    The reference to the Javascript file MUST be a relative path. Therefore if your web page is running in the root of the application and you have a directory called JavascriptLib you will need to put the reference in the page as

    <script language="javascript" type="text/javascript" src="JavascriptLib/MyScript.js" />

    If your web page is below the root and the Javascript library is from the root then normal relative paths apply.

    <script language="javascript" type="text/javascript" src="../JavascriptLib/MyScript.js" />

    If you are using a web user control the reference to the Javascript file is relative to the ASP .Net PAGE that houses the web user control NOT relative to the control.

    There are two options here, either place a directive into the web page that houses the control (as above) or use a script manager to register the library in the web user controls code behind.

    Unfortunately using the script manager you cannot use a path relative to the root of the application, therefore a path such as ~/JavascriptLib/MyScript.js will not be send down with the request. The code you can put into your web user control code behind is as follows

    protected void Page_Load(object sender, EventArgs e)
    {
    // Doesn't work
    // this.Page.ClientScript.RegisterClientScriptInclude("usingBF", "~/Javascript/MyScript.js");
    string url = "~/Javascript/BasicFunctions.js";
    string usableUrl = this.Page.ResolveUrl(url);
    this.Page.ClientScript.RegisterClientScriptInclude("usingMyScript", usableUrl);
    }

    Hope this helps. Good luck with the game


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •