Home >Web Front-end >JS Tutorial >Why Shouldn't I Use Self-Closing Script Elements in XHTML?
Self-Closing Script Elements and XHTML Support
The correct syntax for a script element in XHTML is:
<script src="foobar.js"></script>
Using a self-closing script element like this:
<script src="foobar.js" />
will not be correctly recognized by browsers. This is because the XHTML 1 specification explicitly states that for elements with a content model that is not EMPTY, the minimized form should not be used.
The script element has a content model that is not EMPTY, so the minimized form (with a trailing slash) is not valid. This is specified in the XHTML DTD as:
<!ELEMENT script (#PCDATA)>
Therefore, the use of self-closing script elements breaks the concept of XHTML support. Browsers will not correctly recognize these elements, and they should not be used.
The above is the detailed content of Why Shouldn't I Use Self-Closing Script Elements in XHTML?. For more information, please follow other related articles on the PHP Chinese website!