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>
Community
  • 1
  • 1
Oz Radiano
  • 779
  • 2
  • 11
  • 30

3 Answers3

15

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

18.2.1 The SCRIPT element

...

Start tag: required, End tag: required

Community
  • 1
  • 1
gimel
  • 83,368
  • 10
  • 76
  • 104
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.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

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

dreamlax
  • 93,976
  • 29
  • 161
  • 209
  • 2
    Script tag cannot self-close even in XHTML. It is valid XML but browsers choke on it. – Rex M Apr 30 '09 at 04:33
  • 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 Apr 30 '09 at 05:06
  • Any proper browser that can handle valid XHTML will accept a self-closed SCRIPT element. – dreamlax Apr 30 '09 at 05:08
  • 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 Apr 30 '09 at 11:47
  • 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 Apr 30 '09 at 13:31