<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="/lib/xsl/devedge-1.00/article_en.xsl"?>
<nde:article 
  url="/viewsource/2003/xhtml-style-script/" 
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:nde="http://devedge.netscape.com/2002/de" 
  xmlns:ent="http://devedge.netscape.com/2003/ent"
  xml:lang="en">

  <nde:header>

    <nde:title>
      Properly Using CSS and JavaScript in XHTML Documents
    </nde:title>

    <nde:category>
      TechNote
    </nde:category>

    <nde:pubdate year="2003" month="02" day="28"/>
    <nde:moddate year="2003" month="03" day="14"/>

      <nde:summary>
        Learn why using inline <code>style</code> and <code>script</code>
        tags in XHTML is a problem and how to avoid it.
      </nde:summary>
    
      <!-- 
    <nde:abstract>
    </nde:abstract>
      -->
    
    <nde:channels>
      <nde:channel id="viewsource"/>
      <nde:channel id="xml"/>
    </nde:channels>
    
    <nde:keywords>
      Technote, XHTML, XML, HTML, HTML Comments, Style, CSS, JavaScript, Mime type, content type, application/xhtml+xml, text/html, text/xml
    </nde:keywords>
    
      <!-- 
    <nde:translations>
      <nde:trans url="/viewsource/2003/xhtml-style-script/" title="English translation of this article">English</nde:trans>
    </nde:translations>
      -->

      <!-- 
    <nde:alternates>
      <nde:alt url="/viewsource/2003/xhtml-style-script/xhtml-style-script.pdf" title="PDF version of this article">PDF</nde:alt>
    </nde:alternates>
      -->

    <nde:authlist>
      <nde:author>
        <nde:authname>Bob Clary</nde:authname>
        <nde:authtitle>Technology Evangelist</nde:authtitle>
        <nde:authaffil>Netscape Communications</nde:authaffil>
      </nde:author>
    </nde:authlist>

  </nde:header>

    <!-- 
  <nde:head>
  </nde:head>
   -->

  <nde:content>

    <h2 id="background">Background</h2>

    <p>
      <a href="http://www.w3.org/TR/xhtml1">XHTML<sup><ent:trade/></sup> 1.0 
        The Extensible HyperText Markup Language (Second Edition)</a>
      defines XHTML to be a reformulation of HTML 4 as an XML 1.0 application.
    </p>

    <p>
      XHTML is rapidly replacing HTML 4 among many sites; however, the failure
      of popular web browsers to properly support XHTML, combined with a
      lack of understanding by web authors of the fundamental differences between 
      HTML 4 and XHTML, is creating a growing problem on the web today.
    </p>

    <h2 id="xmlnothtml">XHTML is XML, not HTML</h2>

    <p>
      One of the primary misunderstandings about XHTML is that it is
      <q>just another version of HTML</q>. This misunderstanding is compounded
      by the fact that Microsoft<sup><ent:reg/></sup> Internet Explorer only
      supports XHTML if it is served with Mime type <code>text/html</code>
      rather than the recommended <code>application/xhtml+xml</code>.
    </p>
    
    <p>
      When an XHTML page is served with MIME type <code>text/html</code> it
      is treated by all browsers as if it were nothing more than HTML. However
      when an XHTML page is served with MIME type <code>text/xml</code> or
      <code>application/xhtml+xml</code>, then it should be treated as an
      XML document which must conform to the strict rules for authoring
      and displaying XML.
    </p>

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

    <ol>
      <li>
        <p>
          raw &lt; and &amp; characters are not allowed except inside
          of CDATA Sections (&lt;!CDATA[[ ... ]]&gt;)
        </p>
      </li>
      <li>
        <p>
          Comments 
          (&lt;!<ent:mdash/><ent:mdash/> ... <ent:mdash/><ent:mdash/>&gt;)
          must not contain double dashes (<ent:mdash/><ent:mdash/>)
        </p>
      </li>
      <li>
        <p>
          Content contained within Comments 
          (&lt;!<ent:mdash/><ent:mdash/> ... <ent:mdash/><ent:mdash/>&gt;)
          can be ignored
        </p>
      </li>
    </ol>

    <h2 id="inline">Problems with Inline <code>style</code> and <code>script</code></h2>

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

    <ol>
      <li id="problem-1">
          <p>
            JavaScript typically contains characters which can not 
            exist in XHTML outside of CDATA Sections.
          </p>

    <pre><![CDATA[<script type="text/javascript">
  var i = 0;

  while  (++i < 10)
  {
    // ...
  }
</script>]]></pre>
<p>
  Note that this <a href="examples/problem-1.xhtml">example</a> is <strong>not well formed XHTML</strong> since 
  the use of raw &lt; is only allowed as a part of markup in XHTML or XML.
</p>
</li>
      <li id="problem-2">
        <p>
          Authors who are familiar with HTML, commonly enclose the contents of
          inline <code>style</code> and <code>script</code> tags in comments
          in order to hide the contents of the tags from browsers which do not
          understand them.
        </p>
        <pre><![CDATA[<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>]]></pre>
<p>
  This <a href="examples/problem-2.xhtml">example</a> 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 <code>text/xml</code> 
  or <code>application/xhtml+xml</code> can be problematic.
</p>
  <dl>
  <dt>Mozilla 1.1+/Opera 7</dt>
  <dd>
  Do not apply CSS or execute the JavaScript.
  </dd>
  <dt>Netscape 7.0x/Mozilla 1.0.x</dt>
  <dd>
  Do not apply CSS but does execute the JavaScript.
  </dd>
  <dt>Internet Explorer 5.5+</dt>
  <dd>
  Can not display the document.
  </dd>
  </dl>

        </li>
        <li id="problem-3">
        <p>
          Another <a href="examples/problem-3.xhtml">problem</a> with using comments to surround JavaScript
          in XHTML is related to the problems which can occur if 
          comments contain double dashes (<ent:mdash/><ent:mdash/>).
        </p>
        <pre><![CDATA[<script type="text/javascript">
<!--
  var i;
  var sum = 0;

  for (i = 10; i > 0; --i)
  {
    sum += i;
  }
// -->
</script>]]></pre>
        </li>
      <li>
        <p>
          Properly enclosing <code>script</code> 
          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.
        </p>
        <pre><![CDATA[<script type="text/javascript">
//<![CDATA[
  var i = 0;

  while  (++i < 10)
  {
    // ...
  }
  //]]>]]&gt;<![CDATA[
</script>]]></pre>
        </li>
      </ol>

      <h2 id="examples">Examples</h2>

      <h3>Using CSS rules in inline <code>style</code> within comments</h3>

      <dl>
        <dt id="example-1"><a href="examples/example-1.html">Example 1</a> - XHTML 1.0 Strict as text/html</dt>
        <dd>
          <p>
            This example illustrates the behavior of XHTML served
            as text/html when CSS rules are contained inline and 
            surrounded by comments. 
            This example is supported by Netscape 7.x, Mozilla, Opera 7 and Internet Explorer 5.5+
            which all apply the CSS rules as expected.
          </p>
        </dd>
        <dt id="example-2"><a href="examples/example-2.xml">Example 2</a> - XHTML 1.0 Strict as text/xml</dt>
        <dd>
          <p>
            This example illustrates the behavior of XHTML served
            as text/xml when CSS rules are contained inline and 
            surrounded by comments.
            This example is supported by Netscape 7.x, Mozilla, and Opera 7 but <strong>not Internet Explorer</strong> 5.5+.
            Note that Netscape 7.x, Mozilla and Opera all agree that CSS rules 
            contained inline inside of comments are to be ignored.
          </p>
        </dd>
        <dt id="example-3"><a href="examples/example-3.xhtml">Example 3</a> - XHTML 1.0 Strict as application/xhtml+xml</dt>
        <dd>
          <p>
            This example illustrates the behavior of XHTML served
            as application/xhtml+xml when CSS rules are contained inline and 
            surrounded by comments.
            This example is supported by Netscape 7.x, Mozilla, and Opera 7 but <strong>not Internet Explorer</strong> 5.5+.
            Note that Netscape 7.x, Mozilla and Opera all agree that CSS rules 
            contained inline inside of comments are to be ignored.
          </p>
        </dd>
      </dl>

      <h3>Using CSS rules in external file</h3>

      <dl>
        <dt id="example-4"><a href="examples/example-4.html">Example 4</a> - XHTML 1.0 Strict as text/html</dt>
        <dd>
          <p>
            This example illustrates the behavior of XHTML served
            as text/html when CSS rules are contained in an external file.
            This example is supported by Netscape 7.x, Mozilla, Opera 7 and Internet Explorer 5.5+.
          </p>
        </dd>
        <dt id="example-5"><a href="examples/example-5.xml">Example 5</a> - XHTML 1.0 Strict as text/xml</dt>
        <dd>
          <p>
            This example illustrates the behavior of XHTML served
            as text/xml when CSS rules are contained in an external file.
            This example is supported by Netscape 7.x, Mozilla, and Opera 7 but <strong>not Internet Explorer</strong> 5.5+.
          </p>
        </dd>
        <dt id="example-6"><a href="examples/example-6.xhtml">Example 6</a> - XHTML 1.0 Strict as application/xhtml+xml</dt>
        <dd>
          <p>
            This example illustrates the behavior of XHTML served
            as application/xhtml+xml when CSS rules are contained in an external file.
            This example is supported by Netscape 7.x, Mozilla, and by Opera 7 but <strong>not Internet Explorer</strong> 5.5+.
          </p>
        </dd>
      </dl>

    <h2 id="recommend">Recommendations</h2>

    <ol>
      <li>
      <p>
        Do not use inline <code>style</code> or <code>script</code> 
        in XHTML
      </p>
        <p>
          Replacing inline <code>style</code> and <code>script</code> 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 
          <code>text/html</code> to <code>application/xhtml+xml</code>.
        </p>
        <p>
          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 <code>text/html</code>
          to <code>application/xhtml+xml</code> occurs in the next few years.
        </p>
        <p>
          If you only test your XHTML when it is deployed as <code>text/html</code>,
          then you <em>may</em> be introducing problems such as described in this
          article without realizing it. Moving CSS and 
          JavaScript into separate files is the <em>safe</em> approach with
          regard to changes in how your XHTML is served. 
        </p>
      </li>
      <li>
        <p>
        Follow the XHTML 1.0 
        <a href="http://www.w3.org/TR/xhtml1/#guidelines">HTML 
          Compatibility Guidelines</a>
        </p>
        <p>
          The XHTML 1.0 HTML Compatibility Guidelines help make XHTML documents 
          backwardly compatible with older user agents which do not understand XML.
        </p>
        <p>
          Please note that for pure XHTML documents, you do not need to use the 
          xml-stylesheet processing instruction, but should instead use <code>link</code>
          to reference external files containing CSS.
        </p>
      </li>
    </ol>

    <h2 id="changes">Change Log</h2>

    <h3>March 14, 2003</h3>
    <p>
      Thanks to Martin Honnen for pointing out several errors on my 
      part with respect to permitted characters in XML and for helpful
      comments on the article.
    </p>
  </nde:content>

  <nde:related area="nde">
    <nde:item url="/central/xml/">XML Central</nde:item>
  </nde:related>
  
  <nde:related area="ext">
    <nde:item url="http://www.w3.org/TR/xhtml1/">XHTML<sup><ent:trade/></sup> 1.0 The Extensible HyperText Markup Language (Second Edition)</nde:item>
    <nde:item url="http://www.w3.org/TR/REC-xml">Extensible Markup Language (XML) 1.0 (Second Edition)</nde:item>
    <nde:item url="http://www.xml.com/pub/a/2003/03/19/dive-into-xml.html">XML.com - The Road to XHTML 2.0: MIME Types</nde:item>
  </nde:related>
  
</nde:article>
