Why doesn't this work
<script src="jquery.js"/>
But this works
<script src="jquery.js"></script>
?
Firefox 3.5.8
-
3Dupe of stackoverflow.com/questions/69913/…Brock Batsell– Brock Batsell2010年03月08日 03:27:14 +00:00Commented Mar 8, 2010 at 3:27
-
This question is similar to: Why don't self-closing script elements work?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem.dumbass– dumbass2024年11月20日 11:32:53 +00:00Commented Nov 20, 2024 at 11:32
2 Answers 2
Because:
<script src="jquery.js"/>
is valid XML (including XHTML) but is not valid HTML.
See 18.2.1 The SCRIPT element:
18.2.1 The SCRIPT element
<!ELEMENT SCRIPT - - %Script; -- script statements --> <!ATTLIST SCRIPT charset %Charset; #IMPLIED -- char encoding of linked resource -- type %ContentType; #REQUIRED -- content type of script language -- src %URI; #IMPLIED -- URI for an external script -- defer (defer) #IMPLIED -- UA may defer execution of script -- >Start tag: required, End tag: required
answered Mar 8, 2010 at 3:26
cletus
627k169 gold badges922 silver badges945 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
jdizzle
will the /> trick work (in modern browsers) if the pages Content-Type header is application/xml? I've heard of such black magic before.
cletus
@jdizzle
/> will work if its an XHTML document, which has several ramifications. It has problems with older browsers and (imho) there's no compelling reason to use XHTML so just don't do it would be my advice.The script element is not defined as EMPTY (since you can embed the script directly inside it), therefore in HTML the end tag is required, therefore you cannot have something that is (in tag soup terms) a start tag with a random / character in it representing the entire element.
Hence we have: http://www.w3.org/TR/xhtml-media-types/#C_2
answered Apr 25, 2010 at 20:21
Quentin
949k137 gold badges1.3k silver badges1.4k bronze badges
Comments
default