[whatwg] HTML 5 Script Tag

Cready, James jcready at rtcrm.com
Thu Aug 6 16:14:23 PDT 2009


You make a great point. But whether or not you use the XML/XHTML <syntax/>
or the HTML 4 <syntax> doesn¹t matter much. Since like I showed in my
previous example: the instant you specify a src attribute on your opening
<script> tag the browser will not execute anything inside the tags.
Regardless of whether or not you even specified a value for it.

<script src>
    alert("Hello, my name is Script Src");
    </script>
<script type>
    alert("And my name is Script Type");
    </script>
<script src>
    alert("HEY! Why didn't I get executed!?");
    </script>
<script type>
    alert("Probably because you specified \
    that src attribute inside your brackets");
    </script>
<script src>
    alert("But I didn't specify a value!");
    </script>
<script type>
    alert("Well maybe that's why... \
    The browser got confused because it was \
    expecting a value for your src attribute.");
    </script>
<script src>
    alert("What about you! You didn't specify \
    a value for your type attribute either!");
    </script>
<script type>
    alert("Because the browser Gods hate you.");
    alert("That's why.");
    </script>


I'm also curious as to why you're even allowed to specify an external src on
a <script> tag. Lets compare the difference between CSS and JS when you:

A) Want to include an external file in your HTML:
    CSS: <link rel="stylesheet" type="text/css" href="style.css" />
    JS : <script type="text/javascript" src="script.js" ></script>
or
B) Want to add it inline to your HTML:
    CSS: <style type="text/css">body { margin:0 }</style>
    JS : <script type="text/javascipt">alert('')</script>


It seems like there should be some consistency between the two, either:

A) Allow <link> to pull in all types of external files:
    CSS: <link rel="stylesheet" type="text/css" href="style.css" />
    JS : <link rel="javascript" type="text/javascipt" href="script.js" />
or
B) Allow ONLY <script> and <style> to pull in their external files:
    CSS: <style  src="style.css" type="text/css"></style>
    JS : <script src="script.js" type="text/javascript"></script>


Sometime around 8/6/09 4:55 PM "Eduard Pascual" <herenvardo at gmail.com> typed
up something like:

> 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



-- 
James W Cready




More information about the whatwg mailing list