views:

345

answers:

3

Duplicate

Why don’t self-closing script tags work?

I'm writing an ASP.net page, which have some Javascript code in JS file for client-side authentication.

While using this code prevented from my server-side code from working:

<script src="../../JavaScript/BaseJS.js" type="text/javascript" defer="defer" />

This code somehow worked:

<script src="../../JavaScript/BaseJS.js" type="text/javascript" defer="defer"></script>

Any ideas?

Thanks in advance, Oz radiano.

A: 

<script> can only self-close in XHTML, not HTML.

dreamlax
Script tag cannot self-close even in XHTML. It is valid XML but browsers choke on it.
Rex M
Get your facts straight. XHTML is XML, therefore a self-closing SCRIPT element is valid XML. It passes XHTML validation therefore it is valid XHTML. Just because some browsers choke on it, doesn't mean I am incorrect.
dreamlax
Any proper browser that can handle valid XHTML will accept a self-closed SCRIPT element.
dreamlax
Any browser that implements real XHTML, that is, when sent with the MIME type application/xhtml+xml (e.g., Firefox, Opera, Safari and Chrome) does support self-closing script elements. However, no browser implements *any* self-closing element in HTML, i.e., when sent as text/html, and they shouldn't.
Ms2ger
IE6 (or IE7, or maybe both) don't support XHTML even when the right MIME type is used. I think it always parses it as HTML, where there's no such thing as a self-closing element as you say.
dreamlax
+1  A: 

Am I right in saying you're testing with IE? This is a known bug. (I'm sure there are lots of other pages referring to it too.)

EDIT: In fact, there's another Stack Overflow question about this too.

Jon Skeet
+11  A: 

The HTML4.01 manual section defining the SCRIPT element, states:

18.2.1 The SCRIPT element

...

Start tag: required, End tag: required

gimel