Results 1 to 5 of 5

Thread: Valid XHTML for Local Time script?

  1. #1
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Valid XHTML for Local Time script?

    1) Script Title: Local Time

    2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex6/localtime.htm

    3) Describe problem: This script does not validate for XHTML. It kicks 3 errors, beginning with the complaint "processing instruction does not start with name" about the second ? in '<? print date("F d, Y H:i:s", time())?>'.

    I don't find any existing threads about this. (I do find the equivalent question resolved for the Basic Calendar script here: http://www.dynamicdrive.com/forums/s...ad.php?t=59225 )

    There must be a general thread somewhere about PHP-related solutions for XHTML validation? But I haven't managed to find it.

    Thanks for any pointers.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I think this is a bug in the script, so I'm moving the discussion to the "bug reports" section.
    This is a detailed technical explanation, so skip to the bottom for points most relevant to you. But do read the entire post if you have more questions.

    As explained below, there's no need for validating PHP in XHTML, because it shouldn't be there. It's an order of operations problem. That's why you haven't found any information about fixing this.
    ----

    Actually, that script is written in a really weird way and it should be invalid. (Sorry to whoever wrote it, but it is easy enough to fix.)

    That's not XHTML, and it should not be verified as HTML/XHMTL. It's PHP.

    But here's the bigger situation: in order to get the time from the server, a serverside language must be used. This script offers three alternatives by default: PHP, SSI and ASP. Any of them can tell you the time on the server, then Javascript does the rest.

    However, those three languages should not be mixed, unless you have a good reason to do so. Each language generates HTML, and they're meant to be used independently.

    It's a problem with the order that the languages are executed in. First, serverside languages are executed on the server, then HTML, JS, and CSS execute in the browser.

    Your final Javascript (eg, if you choose "view source" when viewing the page) should have no PHP, ASP or SSI left-- it'll be processed on the server. So it won't ever get validated-- it's irrelevant, just the HTML that it generates.

    But there's a problem: on your server and/or for this page, you probably only have one of those languages. Just looking at the source code, it appears that the Javascript determines which one to use (PHP, ASP, or SSI) to get the value, but that's not what happens. Instead, it selects which already generated value to use in the script. So it's asking your server to generate the time using all three PHP, ASP and SSI methods. This means that almost everyone who uses this script will have two serverside languages that don't do anything. Since they're in Javascript strings, they probably won't hurt anything, but that's not guaranteed.

    Although it would take a little bit more work for completely novice users, it would be much cleaner to remove that aspect of the script and simply offer everyone a list of options for getting the time from the server.

    Remove this section:
    Code:
    var servertimestring=(servermode=="server-php")? '<? print date("F d, Y H:i:s", time())?>' : (servermode=="server-ssi")? '<!--#config timefmt="%B %d, %Y %H:%M:%S"--><!--#echo var="DATE_LOCAL" -->' : '<%= Now() %>'
    And replace it with one of the following:

    PHP:
    Code:
    var servertimestring= '<? print date("F d, Y H:i:s", time())?>'
    SSIL
    Code:
    var servertimestring= '<!--#config timefmt="%B %d, %Y %H:%M:%S"--><!--#echo var="DATE_LOCAL" -->'
    ASP
    Code:
    var servertimestring= '<%= Now() %>'
    Any will work. But it's much better to only have one running since the others will be weird. They could potentially also confuse the server. For example, <% is an alternative tag for PHP (not common but possible) so PHP might try to parse that and get confused.


    Three more points:
    1. You're probably NOT using PHP if you're getting an error about PHP. That's because the PHP isn't actually running so you get the raw PHP code for the validator. Again, don't run the original file's source code through the validator-- use the URL for the final generated HTML (eg, running from your server so the serverside languages execute). But if you are doing that, then it looks like you must be using ASP or SSI so the PHP is just in your way. Just delete it as I showed above (using only ASP or SSI) and you'll be fine.

    2. In order for a serverside language to execute, your server needs to know that it's that kind of page. So usually this is done by changing the extension. If you want PHP, use .php; if you want ASP, use .asp(x); if you want SSI, I think that's usually .shtml (maybe .html too?). Change your page's extension as needed.

    3. That's not well written PHP. It's using the (lazy) short tag PHP brackets that only work on some servers. That may be another reason it won't work for you. Here's the correctly formatted version:
    Code:
    var servertimestring= '<?php print date("F d, Y H:i:s", time()); ?>'
    That format is vastly more compatible with servers (all PHP-enabled servers) compared to the short tags, which are only enabled manually in most cases. And if they are, then often <% %> (ASP-style) tags will also be enabled, so you'd get that conflict too.

    ---
    There's an unrelated problem with validation here: the validator should be ignoring everything in the Javascript (especially in quotes), so you could try the somewhat silly "comments" method, as described here for example: http://www.javascripter.net/faq/hidingjs.htm
    But there is a logical problem with the code (and it might make the JS not work in some cases), so it is worth fixing it.


    ---
    If you need more help, there are three things to do:
    1. Are you validating your generated code (such as through a URL)? Or is it from the file directly? It should be via the final page on your server, not the file.
    2. You may be able to ignore this entirely because it's not an error. It's a false alarm, since it's all inside Javascript.
    3. Which language are you trying to use? If you're not using PHP, then removing it entirely (as I showed above) is fine. Problem solved, in your case. Note that you DO need at least one of those languages to be available or the script simply won't work. (If you have a different serverside language available, such as CGI or JSP, you could use those too.)
    Last edited by djr33; 12-08-2011 at 12:02 AM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks so much!

    This is all very useful info. I'll digest more later -- for now I have belatedly noticed that for this specific scenario I can of course just use SSI instead and hey presto, a lighter page with zero questions about parseability.

    Much obliged for the tutorial.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    If you were simply to make the script external or to comment it beginning and end with the not Character Data tag like so:

    Code:
    <script type="text/javascript">
    /* <![CDATA[ */
    
    /***********************************************
    * Local Time script- © Dynamic Drive (http://www.dynamicdrive.com)
    * This notice MUST stay intact for legal use
    * Visit http://www.dynamicdrive.com/ for this script and 100s more.
    ***********************************************/
    
    var weekdaystxt=["Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"]
    
    function showLocalTime(container, servermode, offsetMinutes, displayversion){
    if (!document.getElementById || !document.getElementById(container)) return
    this.container=document.getElementById(container)
    this.displayversion=displayversion
    var servertimestring=(servermode=="server-php")? '<? print date("F d, Y H:i:s", time())?>' : (servermode=="server-ssi")? '<!--#config timefmt="%B %d, %Y %H:%M:%S"--><!--#echo var="DATE_LOCAL" -->' : '<%= Now() %>'
    this.localtime=this.serverdate=new Date(servertimestring)
    this.localtime.setTime(this.serverdate.getTime()+offsetMinutes*60*1000) //add user offset to server time
    this.updateTime()
    this.updateContainer()
    }
    
    showLocalTime.prototype.updateTime=function(){
    var thisobj=this
    this.localtime.setSeconds(this.localtime.getSeconds()+1)
    setTimeout(function(){thisobj.updateTime()}, 1000) //update time every second
    }
    
    showLocalTime.prototype.updateContainer=function(){
    var thisobj=this
    if (this.displayversion=="long")
    this.container.innerHTML=this.localtime.toLocaleString()
    else{
    var hour=this.localtime.getHours()
    var minutes=this.localtime.getMinutes()
    var seconds=this.localtime.getSeconds()
    var ampm=(hour>=12)? "PM" : "AM"
    var dayofweek=weekdaystxt[this.localtime.getDay()]
    this.container.innerHTML=formatField(hour, 1)+":"+formatField(minutes)+":"+formatField(seconds)+" "+ampm+" ("+dayofweek+")"
    }
    setTimeout(function(){thisobj.updateContainer()}, 1000) //update container every second
    }
    
    function formatField(num, isHour){
    if (typeof isHour!="undefined"){ //if this is the hour field
    var hour=(num>12)? num-12 : num
    return (hour==0)? 12 : hour
    }
    return (num<=9)? "0"+num : num//if this is minute or sec field
    }
    
    /* ]]> */
    </script>
    it would probably be fine.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    For validation, yes. But the underlying issue is that multiple serverside languages should not be arbitrarily mixed. Although they are in JS strings and will likely not cause any problems, if they were ever unintentionally and improperly parsed, that could break the script. As one example, I mentioned above that <% and %> are alternative short tags for PHP and the code inside them would give an error at the moment.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •