- From: Eduard Pascual <herenvardo@gmail.com>
- Date: Thu, 6 Aug 2009 22:55:49 +0200
On Thu, Aug 6, 2009 at 10:36 PM, Cready, James<jcready at rtcrm.com> wrote: > Is there any good reason why a <script> tag with the src attribute specified > can?t be self-closing? > > I understand the need for a <script> tag to have an open and close tag when > you?re executing javascript inline: > > <script type="text/javascript"> > ??alert("Huzzah! I got executed just like you though I would."); > </script> > > However ?best practices? suggest keeping all your scripts external unless > you absolutely need to use inline scripting. So it seems odd to have an open > and close tag when you?re including an external JS file since it won?t > execute anything inside the tags anyway: > > <script src="js/behaviour.js" type="text/javascript"> > ??alert("Boo! I won?t get executed just because the src attribute is > specified. Weak sauce."); > </script> > > I feel like, if you?re including an external JS file, the syntax should look > more like the <link> tag used to include CSS files or even the <img> tag > with it?s src attribute. Both are self-closing and for good reason. Is there > any possibility of including this syntax in the HTML 5 spec or is there some > reason this can?t be done? > > <script src="js/behaviour.js" type="text/javascript" /> The <self-closing /> syntax is an XML feature. The trailing slash is allowed in non-X HTML5 for empty elements only for compatibility with existing XHTML content served as text/html, but it basically means nothing. Allowing the slash for elements that are known to be empty is trivial: it only has to be ignored and things work fine. The <script> case is a bit more complex: it can have contents, so a closing </script> is in principle required. While XML offers the <script ... /> shortcut for code in the form <script ... ></script>, something like <script src="js/behaviour.js" type="text/javascript" /> would actually be parsed as <script src="js/behaviour.js" type="text/javascript">, so everything after that will be taken as part of the script element (and hence won't be shown) until a </script> is found. I agree that, for empty elements, self-closing syntax is better than explicit closing. There could be arguments on self-closing vs. implicit closing; but in the case of requiring a closing tag for an empty element, most people would prefer the self-closing syntax. The main issue here, IMO, is that making this trailing slash relevant within <script> would make things quite messier for the parser, while in the general case it just gets ignored. This makes life harder for implementors and spec writters; and opens the door to some potential bugs. While I think it's good to put authors above implementors and spec writters, bugs tend to hurt the user, and the user is above content authors. In summary: either use <script /> with XHTML5, or use <script></script> in (non-X) HTML. Regards, Eduard Pascual
Received on Thursday, 6 August 2009 13:55:49 UTC