Skip navigation.

Telerik's QuestAll recent postsBook Review: Co-Opetition

Why doesn't <script /> work, but <script></script> does?

To answer this, we need to peek into the most boring reference: the schema.

Tags with content

Remember those DOCTYPE declarations you put at the top of your pages? You can actually see their DTDs in plain and clear at w3c.org. For example, in the XHTML-1.0-Transitional DTD, look at the <script> tag definition:

<!-- script statements, which may include CDATA sections -->
<!ELEMENT script (#PCDATA)>

CDATA simply means “character data”.

CDATA is text that will NOT be parsed by a parser. Tags inside the text will NOT be treated as markup and entities will not be expanded.

PCDATA means “parsed character data”.

Think of [parsed] character data as the text found between the start tag and the end tag of an XML element.

PCDATA is text that WILL be parsed by a parser. The text will be examined by the parser for entities and markup.

Tags inside the text will be treated as markup and entities will be expanded. However, parsed character data should not contain any &, <, or > characters; these need to be represented by the &amp; &lt; and &gt; entities, respectively.

(Source: DTD - XML Building Blocks)

Therefore, you need to insert some content (e.g. JavaScript code) between <script> and </script> tags, or leave it blank if the src attribute points to an external file.

Empty tags

Some elements do not allow content. For example, the all-too-familiar <img> and <link> tags:

<!ELEMENT img EMPTY>
<!ELEMENT link EMPTY>

Therefore it’s incorrect to declare <img src=..." alt="..."></img> since the image element may not contain any character data. Instead, we declare an image as <img src="..." alt="..." />.

Among other empty tags are <br />, <hr />, <meta />, <area />, and <input />.

Validate!

A sure way to catch these little bugs is to run pages through the W3C Markup Validation Service. You can also consult the (x)HTML spec itself. For example, for <script> it explicitly states:

18.2.1 The SCRIPT element
...
Start tag: required, End tag: required

Related reading

See also my post Back to Basics: (X)HTML Specs Made Easy.

Comments

Comment permalink 1 sacranto |
well done
I am confused about it for a long time
Comment permalink 2 Anup Shah |
Nice detail.

There is another reason the empty script tag doesn't work, especially for IE, but often for other browsers:

That is, when XHTML is rendered using the text/html mime type (which is usually the case, because IE (incl. 7) doesn't support the application xhtml+xml mime type (or whatever!).

This means the browser is actually often parsing tag soup (just usually well marked up!), and the empty script tag just looks like a script without a closing tag. (Hence, sometimes you see blank pages in certain scenarios.)

As an aside, it is interesting that Visual Studio 2005 offers XHTML 1.1 strict type, which according to the specs must be served as that XHTML mime type, not text/html, but even IE 7 doesn't support that!
Comment permalink 3 Milan Negovan |
Unfortunately, the schema picker in VS is of very limited use.

Emails and Notifications

Would you like to be notified when somebody responds to this post?  Would you like to have these comments emailed to you?

TrackBacks

Sorry, TrackBacks are not allowed.

Submit your comment

Please enter only text since all HTML tags except hyperlinks will be stripped. Hyperlinks will become live links. Any comments with flaming or offensive language will be deleted. Be courteous to other posters. Thank you.

Your name (required):
Your email (optional):
Your site's URL (optional):
Enter this number
Type in the number above:
Comment (required):