[whatwg] HTML 5 Script Tag

Eduard Pascual herenvardo at gmail.com
Thu Aug 6 16:53:42 PDT 2009


On Fri, Aug 7, 2009 at 1:14 AM, Cready, James<jcready at rtcrm.com> wrote:
> You make a great point. But whether or not you use the XML/XHTML <syntax/>
> or the HTML 4 <syntax> doesn¹t matter much. Since like I showed in my
> previous example: the instant you specify a src attribute on your opening
> <script> tag the browser will not execute anything inside the tags.

You are quite missing the point: see this example:
<!DOCTYPE PUBLIC html>
<html>
    <head>
        <script src="test.js" />
    </head>
    <body>
        <p>Hello World!</p>
    </body>
</html>

Unless you serve this as XHTML (with an XML mimetype), nothing will be
shown on the browser.

Let's see why: text/html triggers the browser's tag-soup parser, which
fixes issues on the fly. The first issue it finds is the straneous
trailing slash on the script, which it just ignores. Then, </head> is
probably taken as literal (as if you had typed &lt;/head&gt;), because
the <head> can't be closed until the <script> is closed. Then that
<body>...</body> will be taken either as a children of the <script> or
as just text (I don't really know *all* the details of error-handling
case by case); but in any case it is content of a <script> inside the
<head>, so there is no chance it will get rendered. Once the parser
encounters the end of the file, it implicitly closes the currently
open tags, adding something like </script></head></html>.
Summarizing, the above sample is taken by any browser as page with an
external <script> that includes some junk content, but with no body at
all.

In the general case, the issue is not the trailing slash for
<self-closing />. The issue is that the browser will ignore everything
from <script ... /> until it encounters a </script>


I like the idea of using <link> to reference external scripts; and it
shouldn't be too hard for vendors to implement. However, there is no
chance to change how browsers handle <script>. Not with so many
millions of pages relying on that behavior. And you can still use
XHTML5 if you want the "/>" to mean something.

Regards,
Eduard Pascual


More information about the whatwg mailing list