Search code examples
htmlw3c-validation

W3C validation error for use of <html> in quoted string: 'document type does not allow element "html" here'


I receive the following error using HTML in a quoted string:

Error Line 63, Column 39: document type does not allow element "html" here

mywindow.document.write("<html><head><title>mydiv</title>");

The element named above was found in a context where it is not allowed. This could mean that you have incorrectly nested elements -- such as a "style" element in the "body" section instead of inside "head" -- or two elements that overlap (which is not allowed).

Am I doing something wrong or is the W3C validator giving bad results?


Solution

  • You wouldn't get that error in an HTML document.

    Presumably, therefore, you are writing XHTML.

    My first piece of advice is: Don't. XHTML is usually more trouble than it is worth. Use HTML 5 instead and that won't be invalid.


    If you need to use XHTML then see the specification:

    Differences from HTML: Script and Style elements

    In XHTML, the script and style elements are declared as having #PCDATA content. As a result, < and & will be treated as the start of markup, and entities such as &lt; and &amp; will be recognized as entity references by the XML processor to < and & respectively. Wrapping the content of the script or style element within a CDATA marked section avoids the expansion of these entities.

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

    HTML compatibility guidelines: Embedded Style Sheets and Scripts:

    Use external style sheets if your style sheet uses < or & or ]]> or > --. Use external scripts if your script uses < or & or ]]> or --. Note that XML parsers are permitted to silently remove the contents of comments. Therefore, the historical practice of "hiding" scripts and style sheets within "comments" to make the documents backward compatible is likely to not work as expected in XML-based user agents.