Wednesday, May 30, 2012

Why don"t self-closing script tags work?

What is the reason browsers do not correctly recognize:

<script src="foobar.js" /> // self-closing script tag

Only this is recognized:

<script src="foobar.js"></script>

Is it breaking concept of XHTML support?

Note: This statement is correct at least for all IE(6-8 beta 2).

Source: Tips4all


  1. XHTML 1 specification says:

    С.3. Element Minimization and Empty Element Content

    Given an empty instance of an element whose content model is not EMPTY (for example, an empty title or paragraph) do not use the minimized form (e.g. use <p> </p> and not <p />).

    XHTML DTD specifies script tags as:

    <!-- script statements, which may include CDATA sections -->
    <!ELEMENT script (#PCDATA)>

    Hope it helps,

  2. To add to what Brad and squadette have said, the self-closing XML syntax <script /> actually is correct XML, but for it to work in practice, your web server also needs to send your documents as properly formed XML with an XML mimetype like application/xhtml+xml in the HTTP Content-Type header (and not as text/html).

    However, sending an XML mimetype will cause your pages not to be parsed by IE7, which only likes text/html.

    From w3:

    In summary, 'application/xhtml+xml'
    SHOULD be used for XHTML Family
    documents, and the use of 'text/html'
    SHOULD be limited to HTML-compatible
    XHTML 1.0 documents. 'application/xml'
    and 'text/xml' MAY also be used, but
    whenever appropriate,
    'application/xhtml+xml' SHOULD be used
    rather than those generic XML media

    I puzzled over this a few months ago, and the only workable (compatible with FF3+ and IE7) solution was to use the old <script></script> syntax with text/html (HTML syntax + HTML mimetype).

    If your server sends the text/html type in its HTTP headers, even with otherwise properly formed XHTML documents, FF3+ will use its HTML rendering mode which means that <script /> will not work (this is a change, Firefox was previously less strict).

    This will happen regardless of any fiddling with http-equiv meta tags, the XML prolog or doctype inside your document -- Firefox branches once it gets the text/html header, that determines whether the HTML or XML parser looks inside the document, and the HTML parser does not understand <script />.

  3. Please read: Sending XHTML as text/html Considered Harmful

  4. Note that IE does not support XHTML parsing. Even if you use an XML declaration and/or an XHTML doctype, IE still parses the document as plain HTML. And in plain HTML, the self-closing syntax is not supported. The trailing slash is just ignored, you have to use an explicit closing tag.

    Even browsers with support for XHTML parsing will still parse the document as HTML unless you serve the document with a xml mime type. But in that case IE will not display the document at all!

  5. In case anyone's curious, the ultimate reason is that HTML was originally a dialect of SGML, which is XML's weird older brother. In SGML-land, tags can be specified in the DTD as either self-closing (e.g. BR, HR, INPUT), implicitly closeable (e.g. P, LI, TD), or explicitly closeable (e.g. TABLE, DIV, SCRIPT). XML of course has no concept of this.

    The tag-soup parsers used by modern browsers evolved out of this legacy, although their parsing model isn't pure SGML anymore. And of course your carefully-crafted XHTML is being treated as badly-written tag-soup/SGML unless you send it with an XML mime type. This is also why...


    ...gets interpreted by the browser as:


    ...which is the recipe for a lovely obscure bug that can throw you into fits as you try to code against the DOM.

  6. If you're serving XHTML as text/html, which you have to for Internet Explorer to do anything, it will be interpreted as HTML 4.01. You can only use the short syntax with any element that permits the closing tag to be omitted. See the HTML 4.01 Specification.

    The XML 'short form' is interpreted as an attribute named /, which (because there is no equals sign) is interpreted as having an implicit value of "/". This is strictly wrong in HTML 4.01 - undeclared attributes are not permitted - but browsers will ignore it.

    XHTML is largely pointless on the web at the moment, because you can't serve it to the majority browser. application/xhtml+xml is not supported in IE8 Beta 2 and I haven't seen anything saying they plan to support it. Stick with HTML 4.01.

  7. The people above have already pretty much explained the issue, but one thing that might make things clear is that, though people use '<br/>' and such all the time in HTML documents, any '/' in such a position is basically ignored, and only used when trying to make something both parseable as XML and HTML. Try '<p/>foo</p>', for example, and you get a regular paragraph.

  8. Unlike XML and XHTML, HTML has no knowledge of the self-closing syntax. Browsers that interpret XHTML as HTML don't know that the / character indicates that the tag should be self-closing; instead they is interpret it like an empty attribute and the parser still thinks the tag is 'open'

    Just as <script defer> is treated as <script defer="defer">, <script /> is treated as <script /="/">.