XHTML is XML, not HTML

One of the primary misunderstandings about XHTML is that it is just another version of HTML. This misunderstanding is compounded by the fact that Microsoft® Internet Explorer only supports XHTML if it is served with Mime type text/html rather than the recommended application/xhtml+xml.

When an XHTML page is served with MIME type text/html it is treated by all browsers as if it were nothing more than HTML. However when an XHTML page is served with MIME type text/xml or application/xhtml+xml, then it should be treated as an XML document which must conform to the strict rules for authoring and displaying XML.

Proper XHTML is an application of XML and as such requires that authors follow strict rules when authoring XHTML. In particular:

Raw < and & characters are not allowed except inside of CDATA Sections (<![CDATA[ ... ]]>).

Comments (<!-- ... -->) must not contain double dashes (--).

Content contained within Comments (<!-- ... -->) can be ignored.

Problems with Inline style and script

Inline style and script tags can cause several different problems in XHTML when it is treated as XML rather than HTML.

JavaScript Contains Characters Which Can Not Exist in XHTML

JavaScript typically contains characters which can not exist in XHTML outside of CDATA Sections. 

<script type="text/javascript">
  var i = 0;
  while (++i < 10)  {
    // ...
  }
</script>

Note that this example is not well formed XHTML since the use of raw < is only allowed as a part of markup in XHTML or XML.

Use of Comments Inside Inline style and script

Authors who are familiar with HTML commonly enclose the contents of inline style and script tags in comments in order to hide the contents of the tags from browsers which do not understand them.

<style type="text/css">
 <!--
  body {background-color: blue; color: yellow;}
 -->
</style>

<script type="text/javascript">
 <!--
  var i = 0;
  var sum = 0;
  for (i = 0; i < 10; ++i)  {
    sum += i;
  }
  alert('sum = ' + sum);
 // -->
</script>

This example illustrates that a conformant browser can ignore content inside of comments. In addition, this example shows how the differences between browsers in handling inline content in text/xml or application/xhtml+xml can be problematic.

Mozilla 1.1+/Opera 7: Do not apply CSS or execute the JavaScript.
Netscape 7.0x/Mozilla 1.0.x: Do not apply CSS but does execute the JavaScript.
Internet Explorer 5.5+: Can not display the document.

Inline style and script Containing Double Dashes

Another problem with using comments to surround JavaScript in XHTML is related to the problems which can occur if comments contain double dashes (--).

<script type="text/javascript">
<!--
  var i;
  var sum = 0;
  for (i = 10; i > 0; --i)  {
    sum += i;
  }
// -->
</script>

The decrement operator (--) in this case causes the failure.

Using CDATA Instead of Comments

Properly enclosing script contents inside of CDATA sections can cause problems in downlevel browsers which do not understand XML.

However, it is possible to combine JavaScript Comments with CDATA sections to allow for backward compatibility.

<script type="text/javascript">
 //<![CDATA[
  var i = 0;
  while  (++i < 10)  {
    // ...
  }
 //]]>
</script>

Recommendations

Do not use inline style or script in XHTML

Replacing inline style and script with external files containing the CSS rules and JavaScript is the best approach for authoring XHTML in a backwardly compatible fashion that will not break if the MIME type of the content is changed from text/html to application/xhtml+xml.

This recommendation may seem rather strong however it is made with the intention of reducing future problems with XHTML content when the transition from serving XHTML as text/html to application/xhtml+xml occurs in the next few years.

If you only test your XHTML when it is deployed as text/html, then you may be introducing problems such as described in this article without realizing it. Moving CSS and JavaScript into separate files is the safe approach with regard to changes in how your XHTML is served.