Loading Scripts in Javascript [duplicate]
Possible Duplicate:
Why do开发者_JAVA百科n't self-closing script tags work?
Why is it that
<script type="text/javascript" src="~/scripts/json2.js"></script>
and
<script type="text/javascript" src="~/scripts/json2.js" />
are different
The latter form doesn't seem to load and if I replace it with the first way it does work correctly.
In XML <foo></foo>
and <foo/>
mean exactly the same thing. So in an XHTML document (if it is interpreted as XML), there is no difference between them.
In SGML, they do not.
HTML was, historically, designed as an SGML application (and predates XML). Browsers have implemented their parsers with this in mind (although they are not complete SGML parsers and some valid syntax is incorrectly handled).
When you serve a document to a browser with a text/html Content-Type, it interprets it as HTML.
Thus <script/>
is treated as a start tag for an element where the end tag is required.
When writing HTML compatible XHTML (i.e. XHTML that is served as text/html) the guidelines recommend that elements which are defined as EMPTY (and thus the end tag is forbidden) be expressed as <foo/>
and all other elements be expressed as <foo></foo>
.
Since the <script>
element can have content (an inline script), the end tag is not forbidden, so the explicit end tag is needed.
Probably because is not a single thingy tag (I forgot what they are called) like BR or HR. Script tags always need to be opened and closed.
精彩评论