467,874 Members | 1,728 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,874 developers. It's quick & easy.

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)
http://www.safalra.com/hypertext/
Jul 20 '05 #1
  • viewed: 2386
Share:
1 Reply
/Safalra/:
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>


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
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Sims | last post: by
17 posts views Thread by lkrubner | last post: by
14 posts views Thread by dmh2000 | last post: by
2 posts views Thread by Vivek Kumar | last post: by
5 posts views Thread by Carl J. Van Arsdall | last post: by
100 posts views Thread by jacob navia | last post: by
3 posts views Thread by The Bicycling Guitarist | last post: by
reply views Thread by jack112 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.