howa wrote:
1.
<script type="text/javascript">
alert("test");
</script>
Because in XHTML the content of a script element is PCDATA the above is
completely acceptable, but if the script contained characters that were
meaningful to the XML parser they would need to be transformed into
their corresponding mark-up entities. Most real scripts would contain
such characters.
2.
<script type="text/javascript">
<!--//
alert("test");
//-->
</script>
In PCDATA processing instructions are interpreted as processing
instructions and so comments they contain are interpreted as comments.
Comments may be stripped out by an XML parser but even if they were not
there should be no expectation of their contents being passed on to a
javascript interpreter. The above is just wrong in an XHTML (and all
otehr XML) context(s).
3.
<script type= "text/javascript">/*<![CDATA[*/
alert("test");
/*]]>*/</script>
Wrapping <![CDATA[ ... ]]around the script contents means that
characters that may be significant to the mark-up will be disregarded
(except the "]]>" sequence, which is a potentially valid construct in
javascript, such as in - if(a[b[c]]>2){ ... } -)).
It is utterly redundant to javascript style multi-line comments around
the CDATA mark-up in an XHTML document.
which one you prefer? (assume xhtml transitional)
The preferable approach is to realise that while IE browsers do not
support XHTML that mark-up flavour has no value in a commercial context
as something to be sending to web browsersl. And so use only HTML, where
the contents of SCRIPT elements are CDATA by definition.
If XHTML is to be used the only sensible option is to import the scripts
into the document, and so avoid all questions of what the XML parser may
make of the characters used.
Richard.