views:

75

answers:

3

I've noticed many sites use this, without closing the tag.

 <script type="text/javascript" src="editor.js">

This style is also recommended but is longer:

 <script type="text/javascript" src="editor.js"></script>

Can I write it like this? Is it valid or is there a better way?

 <script type="text/javascript" src="editor.js" />
+7  A: 

You always want to use

<script type="text/javascript" src="editor.js"></script>

Some browsers do not allow self-closing script tags.

For more information, see Why don't self-closing script tags work?

Daniel Lew
What? Self closing tags are part of XML, and therefor part of XHTML, why don't the browsers support it?!?
Malfist
IE doesn't truly support XHTML, and given IE's market share you cannot simply ignore these users. (Browsers not supporting features they ought to support is common practice. If you're going to do web development, you have to be prepared for this unfortunate situation.)
Daniel Lew
+1  A: 

Use the second option. Not all browsers support the self-closing style.

Chris Pebble
And indeed the results can be very confusing. Definitely #2
David Caunt
+1  A: 
<script type="text/javascript" src="editor.js">

This is invalid, and will break things.

<script type="text/javascript" src="editor.js"></script>

This is fine.

<script type="text/javascript" src="editor.js" />

This is fine so long as you are using XHTML that is not HTML Compatible. This means you need to serve the XHTML with an XML content-type (preferably application/xhtml+xml) and forget about supporting Internet Explorer (except with a separate document).

David Dorward