Re: html code in <script>

David Woolley wrote:
> Paul Kieffer wrote:
>> www.ucog.org/news1.php?strArticle=NEWS/2007-10-20.htm
> 
> The page is invalid XHTML, which means it is also invalid under Appendix 
> C rules.  If this were served as XHTML, it would need a CDATA section 
> around the script; you could also use entities to protect the "<", etc. 
>   If it were served as proper HTML, best practice would be to put an 
> HTML comment around the script content.  None of these options are 
> available under Appendix C rules (you cannot use entities to protect 
> scripts in HTML as they are implicitly CDATA sections), so you must have 
> the script out of line.

In fact, one can write something like

<script type="text/javascript">
//<![CDATA[
 ...
//]]>
</script>

and it will work in both HTML and XHTML mode.
(http://developer.mozilla.org/en/docs/Properly_Using_CSS_and_JavaScript_in_XHTML_Documents)

Of course, if you mind about *old* browsers, you need to write something
like
<script type="text/javascript"><!--//--><![CDATA[//><!--
        ...
//--><!]]></script>
(http://www.hixie.ch/advocacy/xhtml)


> My advise would be to revert to HTML 4.01 and use HTML comments to 
> protect the scripts against earlier browsers.  Many people think that 
> Appendix C (serving XHTML as text/html) was a bad idea; it relies on 
> certain non-compliances that are common in text/html browsers.

I agree. If you want to use XHTML, the best way is to check the
Accept-header and if it doesn't contain "application/xhtml+xml" then the
server must encode the content as a real HTML document. Sending an XHTML
file with "text/html" content type will not win anything over real HTML
and can cause problems in the worst case.

-- 
Mikko

Received on Wednesday, 14 November 2007 13:45:07 UTC