1

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>
1
  • this is part of the standard, script tags cannot be self closed. Commented Feb 27, 2009 at 2:00

3 Answers 3

14

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

18.2.1 The SCRIPT element

...

Start tag: required, End tag: required

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

5 Comments

Script tag cannot self-close even in XHTML. It is valid XML but browsers choke on it.
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.
Any proper browser that can handle valid XHTML will accept a self-closed SCRIPT element.
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.