Can the <script> tag be non-closed?

I had this code on my website

<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"/> <script type='text/javascript' src='/lib/player/swfobject.js'></script> 

swfobject did not work (did not load).

After changing the code:

 <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script> <script type='text/javascript' src='/lib/player/swfobject.js'></script> 

It worked fine.

The document is parsed as HTML5.

I think this is funny. Well, I provided a tag that is closed, and the self-closing tag does not match. So I would understand if jQuery might not load (although I find it funny).

But I do not understand that jQuery is loading, but the next correctly written tag is not?

+46
html html5 xhtml
Dec 25 '10 at 21:39
source share
4 answers

HTML has tags that always self-close. For example, <hr>Some content here</hr> makes no sense. Similarly, there are tags that cannot be self-closed. The <script> is one of them.

I am not sure about the lack of self-closing <script> tags, but the reason may be that the tag should always contain code inside. Again, I'm not sure.

+45
Dec 25 '10 at 21:45
source share

Because it is parsed as:

Line 1: The start tag for the script

 <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"/> 

Line 2: JavaScript (really broken JavaScript!) To execute if the external script specified in line 1 is not loading

  <script type='text/javascript' src='/lib/player/swfobject.js'> 

Line 3: The end tag for the script is running on line 1

 </script> 

Well, I provided a tag that is closed, and the closing tag itself does not match.

They are the same (if there is no content), but only in XML documents. The XHTML document used as application / xhtml + xml is an XML document. In an HTML document, thanks to a legacy of incorrect implementations by browsers, a self-closing tag is just the start tag (and this is normal only when the end of the tag is forbidden).

+24
Dec 25 '10 at 21:50
source share

David Dorvarg's answer explains this from one angle, but there is a deeper reason why you cannot do this:

The slash at the end of the tag does not make it self-closing in HTML

Self-closing syntax is part of the XML. In a normal HTML document, this does not matter.

+19
Dec 26 '10 at 22:24
source share

@Joe Hopfgartner: You changed the code to check if

 <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js" /> <script type="text/javascript" src="/lib/player/swfobject.js" /> 

works?; -)

Update

Run the code and the <p> element is hiding, so ... does it look like it works?

HTML

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> <title>questions/4531772</title> <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"/> <script type="text/javascript" src="4531772.js"/> </head> <body> <p class="test">Testing...</p> </body> </html> 

JavaScript (4531772.js)

 $(document).ready(function() { $('.test').hide(); }); 
+3
Dec 25 '10 at 21:58
source share



All Articles