1

Possible Duplicate:
Why don’t self-closing script tags work?

I had this bit of javascript code inside a <head> element.

<script src="jquery-1.3.2.js" type="text/javascript" />

<script type="text/javascript">
    $(document).ready(function() {
        $("#welcome").addClass("centered");
        $("#created").addClass("centered");
    });
</script>

Which refused to work until I used an explicit end script element:

<script src="jquery-1.3.2.js" type="text/javascript"></script>

Why is there a difference?

EDIT: the entire header was:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link rel="stylesheet" href="css/blueprint/screen.css" type="text/css" media="screen, projection" />
    <link rel="stylesheet" href="css/blueprint/print.css" type="text/css" media="print" />
    <!--[if lt IE 8]><link rel="stylesheet" href="css/blueprint/ie.css" type="text/css" media="screen, projection" /><![endif]-->
    <link rel="stylesheet" href="css/blueprint/src/typography.css" />
    <link rel="stylesheet" href="css/common.css" />

    <script src="jquery-1.3.2.js" type="text/javascript" />

    <script type="text/javascript">
        $(document).ready(function() {
            $("#welcome").addClass("centered");
            $("#created").addClass("centered");
        });
    </script>
</head>

I don't understand why the script element needs an explicit end element but the link element doesnt.

Community
  • 1
  • 1
Ferruccio
  • 98,941
  • 38
  • 226
  • 299

3 Answers3

3

Yeah, you always need to end script elements like that. It doesn't support the XHTML format of ' />' to end the tag.

Noon Silk
  • 54,084
  • 6
  • 88
  • 105
  • I've noticed this was the case, and now close all js-related tags this way, but is there any particular reason? I'm aware it's probably a duplicate question, but that's why this is just a comment. – Marc Bollinger Aug 19 '09 at 01:06
  • I don't know exactly, but my guess would be it's due to compatibility. Quirksmode has some info : http://www.quirksmode.org/bugreports/archives/2006/06/MSIE_script_tag_failure_in_valid_xhtml.html – Noon Silk Aug 19 '09 at 01:08
  • Technically the `/>` format is valid in XHTML, but never in HTML—well, it actually has an obscure, rarely-implemented meaning. And even if you think you’re writing XHTML, odds are it’s being served as `text/html` and so the browser is treating it as HTML. The fact that tags like `
    ` work (the HTML version is just `
    ` with no closing tag) is only because HTML parsers are forgiving.
    – Nate Aug 19 '09 at 02:12
  • If an element has an empty content model then the XML empty-element syntax is acceptable. If an element does *not* have an empty content model, the XML syntax is not acceptable. Check the HTML 4.01 or XHTML 1.0 DTDs for details of which elements have an empty content model. – NickFitz Aug 19 '09 at 09:35
2

What is the doctype of your document? Technically minimized tags like your first example are an XML-only thing. In most cases HTML allow them, but the correct HTML is your second example.

Licky Lindsay
  • 1,048
  • 6
  • 10
1

Certain elements like script and iframe dont work correctly in all browsers unless you have a closing tag, even if there is no content inside. While I agree that this is stupid, its just one more of those inconsistencies that web developers need to be aware of.

I would imagine that once browser vendors implement actual XHTML (so we can send XHMTL as application/xhtml+xml not text/html) that at that point they would also fix inconsistencies like this. Or maybe I'm just an optimist.

Darko
  • 38,310
  • 15
  • 80
  • 107
  • Good luck with that, with HTML5 out, XHTML will most likely NEVER reach that point. – micmcg Aug 19 '09 at 07:07
  • AFAIK they've only suspended work on XHTML2 until HTML5 is done, though I sympathize with your pessimism :) – Darko Aug 19 '09 at 09:08