Friday, September 22nd, 2006

Why doesn’t <script /> work?

Category: HTML

Admit it. One time in your youth you did the following:

javascript

  1. <head>
  2. <script src="..." type="text/javascript" />

Then, when you tested your app you quickly found that your JavaScript wasn’t being called. When you realise the problem is that you needed to explicitly close the script tag with </script> you wonder for a second how you can do <img …. /> and the like, but that doesn’t work for script (and canvas and div and …).

The Safari folks have spelled it all out in Understanding HTML, XML and XHTML. The articles goes into the differences, and you end up with the obvious conclusion that <img …. /> works as:

It’s important to note that this is kind of a hack, and depends on the de facto error handling behavior of HTML parsers. They don’t really understand the XML self-closing syntax, but writing things this way makes them treat / as an attribute, and then discard it because it’s not a legal attribute name. And if you tried to do something like

, they wouldn’t understand that the div is supposed to be empty.

Posted by Dion Almaer at 1:18 pm
9 Comments

+++--
3.8 rating from 39 votes

9 Comments »

Comments feed TrackBack URI

After reading the Surfin’ Safari article and others on the topic, our company is considering “reverting” from XHTML 1.0 to HTML 4.01. Are others doing the same? The only real reason we could come up with for staying with XHTML was marketing, you know, so we appear to be up on the latest and greatest. I’d be interested to hear technical arguments for staying with XHTML.

Comment by Andrew Hedges — September 22, 2006

is webkit.org the same as the Safari folks?

Comment by dc — September 22, 2006

Cool, very informative… @dc – I think safari is bult on webkit.

Comment by Mario — September 22, 2006

I don’t agree with the articles serving up HTML *at all*. To me, the article is simply saying “Our software ‘Webkit’ can’t handle XHTML as well as we’d like, so for the time being, can you publish as HTML? It’ll work better!”

XHTML has at the very least added formal structure to web development! Now I know why I don’t use Safari!

Comment by Ian — September 22, 2006

I remember there was a blog post early that shows an IE error where if you have a simple , IE will duplicate the text in front of it.

Comment by Simon Jia — September 22, 2006

Dion, you missed the point of the article, I am afraid.

The fact that <img /> works and <script /> does not work has nothing to do with the error handling behaviour of the HTML parser.

The HTML DTD clearly states:
<!ELEMENT IMG – O EMPTY — Embedded image –>
<!ELEMENT SCRIPT – – %Script; — script statements –>
<!ENTITY % Script “CDATA” — script expression –>

See the EMPTY at the IMG definition?

The paragraphs from the blog article only explain, why you can use <img /> in documents served as content-type text/html. Why the whole “XHTML doctype, but served as text/html”-mess works at all.

Comment by Martin Bialasinski — September 22, 2006

Hmm guess I should stop cursing my

Comment by Vance Dubberly — September 22, 2006

Completely agreed with Martin Bialasinski. The sole reason for it to happen is because people are not serving XHTML documents, thus making the browsers still parse them as HTML (have you ever tried validating your websites as what they really are, HTML?). The slash inside an HTML tag has a different meaning (inherited from SGML), but most browsers ignore it (or else they would break with all fake-XHTML websites).

Since IMG tags have no content, there’s no difference for you if the “/” is being interpreted or not.

Comment by Daniel Luz — September 23, 2006

Logically thinking, it’s expected for IMG to be a closed element as it never has any nested elements or text inside of it.

Following this logic, SCRIPT could be either/or given that you can have it as an “empty” (and therefore closed) element by using SRC= ..but if the code is inserted inline, then obviously a separate closing tag is needed. Looks like SCRIPT should always have a separate closing tag, based on what the article says.

Comment by Scott Schiller — September 23, 2006

Leave a comment

You must be logged in to post a comment.