Why Don't Self-Closing Script Elements Work

Why don't self-closing script elements work?

The non-normative appendix ‘HTML Compatibility Guidelines’ of the 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 elements as:

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

Can the script tag not be self closed?

In HTML, there are tags which are always self-closed. For example, <hr>Some content here</hr> does not make any sense. In the same way, there are tags which cannot be self-closed. <script> tag is one of them.

I am not sure about the reason of no self-closed <script> tags, but the reason might come from the fact that the tag was intended to always contain code inside. Again, I'm not sure.

Why some HTML5 tags must have a starting and ending tag instead of self closing them with /?

From the w3c:

A Self-closing tag is a special form of start tag with a slash
immediately before the closing right angle bracket. These indicate
that the element is to be closed immediately, and has no content.
Where this syntax is permitted and used, the end tag must be omitted.
In HTML, the use of this syntax is restricted to void elements and
foreign elements. If it is used for other elements, it is treated as a
start tag. In XHTML, it is possible for any element to use this
syntax. But note that it is only conforming for elements with content
models that permit them to be empty.

The examples you listed would generally contain content, JavaScript, or other elements, so having a proper start and end tag would delimit the scope of those elements/tags.

Is it needed to close the script tag when it's used for embedding an external script file?

No, the <script> tag requires separate opening and closing tags, although you may get away with using a self-closing tag if the page's Content-type is set to application/xhtml+xml.

See the HTML 4.01 specification

Aurelia: self-closing require element does not work

According to the HTML specification, there are just a few void elements (elements that only have a start tag), which are:

area, base, br, col, embed, hr, img, input, keygen, link, meta, param, source, track, wbr.

https://www.w3.org/TR/html5/syntax.html#void-elements.

Aurelia uses the browser's native DOMParser, which means it follows the same set of standards.

Is there a rule to remember if a tag is self-closing or not?

Self-closing tags are only used, when there can not be Child-Elements in the Tag.

For example <script type="text/javascript">...</script> could have some code in it.

But <link rel="stylesheet" type="text/css" href="xxx.css" /> could not ever have any sub-elements, so it's self-closing.

Find a list of self-closing Elements here: http://www.w3.org/TR/html5/syntax.html#void-elements

  • area
  • base
  • br
  • col
  • embed
  • hr
  • img
  • input
  • keygen
  • link
  • meta
  • param
  • source
  • track
  • wbr

... only these tags are allowed to be self-closing.

The rule to remember would be almost no tags, except the ones in the list above. Just remember the most important ones for yourself.

Close HTML Script Tag

The concept of self-closing tags is an XML concept. You can't use them in HTML. (You can use them in XHTML, but only if the document is served with an XML content-type, not if it is served as text/html).

In HTML some elements (such as <img>) cannot have any content, so they don't have end tags).

Since a script can have a src attribute or the script be can inside the element, <script> is not one of them.

(HTML 5 allows a / character to appear at the end of a start tag for an element that is defined as EMPTY, but it is just sugar for people addicted to XML and has no meaning in the language).



Related Topics



Leave a reply



Submit