Perferred way to embed javascript into xhtml

H

howa

1.

<script type="text/javascript">
alert("test");
</script>

2.

<script type="text/javascript">
<!--//
alert("test");
//-->
</script>

3.

<script type= "text/javascript">/*<![CDATA[*/
alert("test");
/*]]>*/</script>



which one you prefer? (assume xhtml transitional)


thanks.
 
H

howa

howa said the following on 4/15/2007 4:07 AM:




<script type="text/javascript">
alert("test");
</script>
2.
<script type="text/javascript">
<!--//
alert("test");
//-->
</script>
3.
<script type= "text/javascript">/*<![CDATA[*/
alert("test");
/*]]>*/</script>
which one you prefer?

<script type="text/javascript" src="myExternalFile.js"></script>

assuming the scripts will only be called once for that page, external
JS seems not a good solutions.
xHTML isn't a good choice on the web though.

maybe until HTML5 ?
 
R

Richard Cornford

howa said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top