1

Does anyone know of a good reference or rule of thumb for when to finish a tag properly <script></script> or when to use the simpler <script />.

Perhaps a couple of examples of which is best for, eg. <input></input> vs <input />, <script /> vs <script></script> etc etc.


Please feel free to edit and rephrase my question if I don't have the terminology quite right
Jamie
  • 379
  • 1
  • 5
  • 22
  • http://stackoverflow.com/questions/97522/what-are-all-the-valid-self-closing-tags-in-xhtml-as-implemented-by-the-major-br – Sarfraz Feb 12 '11 at 21:59
  • When served as text/html, `` is especially evil. It will self-close the script element in Safari, but all other browsers will treat it as a script start tag. – Alohci Feb 12 '11 at 22:39

3 Answers3

3

Never use <element /> in HTML since, depending on the version of HTML you are using and where you put it it either:

  • doesn't mean what you think
  • is a syntax error
  • is optional and therefore a waste of time

The list of elements in HTML 4.01 has a column which shows you when start and end tags are optional or forbidden.

If you are writing XHTML then use <element /> when, and only when, the end tag is Forbidden in HTML 4. This is part of the HTML compatibility guidelines. (If you are one of the very tiny number of people serving XHTML as application/xhtml+xml (thus excluding users of IE 8 and lower) then you can use the syntax on any element).

If you are writing HTML 5. Then you can use that syntax under the same rules as XHTML - but it is optional and therefore I wouldn't bother.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Usually it's your preference, although it also depends on what type of html you're going for. 4 vs. 5, strict, loose or transitional.

Although I would recommend against doing <script />. It seems that if you do the short version, IE doesn't actually download your JS file! <script></script> works though.

Andy
  • 8,432
  • 6
  • 38
  • 76
  • It is only a matter of preference when writing XHTML served as `application/xhtml+xml` (which is almost never). – Quentin Feb 12 '11 at 22:08
  • Html is much less strict than XHtml, so how does it not boil down to targeted doctype followed by preference? – Andy Feb 13 '11 at 00:14
  • HTML is not "less strict" than XHTML. It makes some things optional, and doesn't require parsers to stop on certain errors. The rules are still well defined. If it was a matter of preference then you could use `` and `` interchangeably (in text/html you can't since some browsers will treat everything after `` as the content of the script element), ditto `
    `/`
    ` (the former gets treated as 2 `
    `s in some browsers). The preference only comes if you are using true XHTML (almost nobody does), otherwise you are bound by the language rules.
    – Quentin Feb 13 '11 at 11:47
0

It really depends on your document definition type. If you are unsure which one best fits your doc type then you could always just run it through a validator which will complain if it sees anything that doesn't fit your doc type. For example:

http://validator.w3.org/

New Guy
  • 8,606
  • 1
  • 15
  • 12
  • 1
    There are plenty of places you can use that syntax in HTML where it is wrong but valid (since it doesn't mean the same as in XHTML) and since XHTML is usually served as text/html there are plenty of places that are valid but will break the document (`` causing the entire rest of the page to be treated as invalid JS and not rendered as HTML in IE for example) – Quentin Feb 12 '11 at 22:03
  • The validator I posted will validate XHTML as well and yes "Valid" just means it is valid code. Not necessarily the right code to use. They OP asked if there was a rule of thumb to follow. A good rule of thumb to follow is to follow your doc type, which can be validated using the validator I posted. – New Guy Feb 12 '11 at 22:36
  • The validator does not perform tests based on the Compatibility Guidelines, so it is a dreadful rule of thumb. I gave an example above where the validator will say everything is wonderful but, if interpreted as HTML, at least one major browser will fail to render any content in the page. – Quentin Feb 13 '11 at 11:41
  • @DavidDorward Judging from your post I see that you clearly know a lot about this subject and I respect that. However, the OP and others reading this may not. In my opinion a validator can be a very useful tool for those just starting out. After all, if you code does not validate how could it possibly meet any compatibility guidelines (in general that is, I'm sure you can find some off the wall case where this is not true). That being said I believe the answer you gave best fits the posted question. Upvoted. – New Guy Feb 13 '11 at 14:19