tags:

views:

43

answers:

1

I'd like to output scripts into HTML without knowing whether it's going into a HTML4, HTML5 or XHTML document. The best format I can find is:

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

That validates in all three schemes, but will it work in all browsers in all three doctypes?

+3  A: 

It's fine.

In HTML it is parsed as JavaScript, so treated as comments. In XHTML it is parsed as XML, so treated as CDATA markers.

(This is rather essential to the way the HTML compatibility guidelines for XHTML work to deal with the issue of script elements being handled differently in the two languages).

David Dorward