Comments and scripts

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Safalra

    Comments and scripts

    If I understand the specification corrently, comments are delimitted
    by -- and --, but these delimitters can only occur in a block marked
    by <! and >. Is this correct?

    If I try to validate something like <!-- a -- b -- c --> then the
    validator obviously complains about b: "invalid comment declaration:
    found name start character outside comment but inside comment
    declaration". Is there anything that can occur in such a place in
    HTML? I wondered about the DOCTYPE statement, but it failed to
    validate.

    It's common to 'comment out' Javascript, but presumably in the
    following:

    <script type="text/javascript">
    <!-- begin comment out
    a=5;b=5;
    b--;
    if (a>b) document.write( "<p>a is greater than b</p>");
    // end comment out -->
    </script>

    the comment ends at the -- in 'b--;', the block containing the comment
    ends at the '>' in 'if (a>b)', and the script elements ends at the
    '</' in the '</p>'. If this occured in the document body, we would
    see:

    p>"; // end comment out -->

    Am I correct in this assessment? (I know that real browsers cope with
    horrible mess like this, I'm talking from a more theoretical
    viewpoint.)

    (I'm asking this as I'm thinking about automated HTML neatening -
    presumably the correct approach is to removing the commenting out (as
    pretty much all browsers now know not to display the contents of a
    script element), parse the javascript, albeit at a very shallow level,
    and escape occurances of </. Either that or move the javascript to an
    external file, of course.)

    --
    Safalra (Stephen Morley)

  • Stanimir Stamenkov

    #2
    Re: Comments and scripts

    /Safalra/:
    [color=blue]
    > It's common to 'comment out' Javascript, but presumably in the
    > following:
    >
    > <script type="text/javascript">
    > <!-- begin comment out
    > a=5;b=5;
    > b--;
    > if (a>b) document.write( "<p>a is greater than b</p>");
    > // end comment out -->
    > </script>[/color]

    Put your SCRIPT elements in the HEAD (and don't use comments) or
    link the code externally (<script src="..." ...).

    Browsers usually overcome the "<!--...--...-->" situation accepting
    only "<!--" and "-->" for start and end delimiters of comments.

    For SCRIPT elements which you need in the document body you could
    presume UAs know how to handle them (no matter if they support
    scripting or not), just narrow down the script code to a single
    function call linking to code defined already in the HEAD or
    possibly in an external file, so the mess would be minimal if one is
    using a browser which doesn't know how to handle SCRIPT elements and
    would render their content.

    Note also the incompatibility of this technique of hidding SCRIPT
    element's content with XHTML <http://www.w3.org/TR/xhtml1/#C_4>.

    --
    Stanimir

    Comment

    Working...