Search code examples
htmltheory

is there a reason why <script> has to have a separate closing tag?


To start, I know that I have to have the </script> tag, and there are existing questions about that. The question isn't whether or not I need a closing tag. My question is: why was it designed this way?

The source of confusion for me comes from looking at the <link /> element - it appears to have similar functionality (importing external text files and defining their type) but has the self-closing property (which we see in other but not all element types). I may be oversimplifying things, but I don't understand why one external reference element should use a style that is different from another similar (obviously not the same) external reference element.

It looks like this doesn't change in the HTML5 draft either. I just want to understand the reasoning behind it so I can have a better/deeper understanding of basic HTML and why it works the way it does.


Solution

  • why was it designed this way?

    It must have an explicit end tag because you can have inline script:

    <script>
        foo();
    </script>
    

    Having a forbidden end tag wouldn't work (since then you couldn't have content). Having an optional end tag would be more trouble then it is worth (since the element contains CDATA … that might actually make it impossible to have an optional end tag, I don't know that bit of SGML well enough to say).

    It doesn't use <link> because it was a product of the browser wars and not something that was discussed in the W3C before being introduced.

    It looks like this doesn't change in the HTML5 draft either.

    It wouldn't be backwards compatible.