3

Why the following line is not working in many browsers (mozilla, IE)?

<script src="jQuery.js" />

Why we must set it like this?

<script src="jQuery.js"></script>

These days I convert my project from XHTML to HTML5 and I face some minor but disturbing incombatibilities.

4
  • 1
    There have always been compatibility issues here; a full closing tag for <script> has always been a good idea, even in the xhtml days, no matter how ugly it looks. Commented Aug 15, 2013 at 9:57
  • 5
    Well, the specification says: "A script element must have both a start tag and an end tag." If ask why the decision was made, you have to ask someone who worked on the specification. Commented Aug 15, 2013 at 9:59
  • 1
    It's because <script></script> tag is not self closed. See more details here [stackoverflow.com/questions/4531772/… [1]: stackoverflow.com/questions/4531772/… Commented Aug 15, 2013 at 10:14
  • "I face some minor but disturbing incombatibilities." - this disturbs you? God help you if you ever, I don't know, watch the news or something. Commented Aug 15, 2013 at 10:44

2 Answers 2

4

While a script element can have the content loaded from an external URL via the src attribute, it can also have the code inline (as a child node). This means it is not an EMPTY element so can't require that the end tag be omitted.

In HTML 5 the / at the end of a start tag does not mean "This is a self-closing tag", it is just syntactic sugar for people who have written too much XML or are using XML syntax highlighters.

Even if you were still writing XHTML, you probably couldn't use the <script /> syntax as it is not HTML compatible.

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

Comments

0

HTML requires that the <script> must be closed with </script>. In HTML, <script /> always has meant an opening <script> tag with the attribute /; with unknown attribute names ignored. Entities are not parsed within an embedded script in HTML mode, and the script ends at the earliest occurrence of </script string, or sometimes at </, depending on the browser.

The only exception to these rules was the XHTML parsed in XML mode - there self-closing script tags would have worked; but since this really never worked in all the browsers anyway (that is, XHTML), and XHTML served with Content-Type: text/html means that it should parsed using HTML rules, you shouldn't use it even in "XHTML".

To clarify, Serving HTML & XHTML says the following:

To send XHTML markup to a browser with a MIME type that says that it is XML, you need to use one of the following MIME types: application/xhtml+xml, application/xml or text/xml. The W3C recommends that you serve XHTML as XML using only the first of these MIME types – ie. application/xhtml+xml.

When a browser reads XML it uses an XML parser, not an HTML parser.

Unfortunately, up to and including version 8, Internet Explorer doesn't support files served as XML, although a number of other browsers do. To get around the fact that not all browsers support content served as XML, many XHTML files are actually served using the text/html MIME type. In this case, the user agent will read the file as if it were HTML, and use the HTML parser.

Since the browser considers the XML to actually be HTML, you need to take into account some of the differences between the two formats when writing your XHTML code, to ensure that the differences between XML and HTML syntax do not trip up the browser. This includes different ways of declaring the character encoding or language declarations inside the document.

Thus, to account for differences in browsers, you must close your script tags explicitly if you want your files to be able to be processed using HTML parsers; this is expectation for many, if served with text/html content type.

2 Comments

Nope, this was about serving with XML content type, XHTML served with HTML content type is still HTML.
Yup. XHTML served with an XML content type has worked cross browser since IE added support for it in IE9.

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.