tags:

views:

156

answers:

2

Are there any pros to use HTML 4.01 strict over XHTML 1.0 strict (content="text/html)?

+4  A: 

Sure. Your code will be smaller, since you don't need to include the extra " characters on one-word attributes, / characters on self-closing elements, you can leave out various opening and closing tags, such as </p>, </li>, and even <html>, <head>, <body>, and so on are optional in HTML 4.01 (and HTML5). See Optimizing HTML and Optimizing Optimizing HTML for a few tips on stripping down your HTML; many of these pieces of advice work only in HTML 4.01 or HTML5.

Now, not everyone needs to minify this much; but if you do, using HTML 4.01 (or HTML5) instead of XHTML can be beneficial.

Also, Internet Explorer doesn't actually support XHTML as XHTML; if you send it as application/xhtml+xml, it will just try to download it. So, if you use XHTML, you need to send it to IE as text/html, which will make the browsers just interpret it as HTML. For a somewhat outdated discussion of why XHTML is a bad idea, see XHTML Considered Harmful by Ian Hickson, the current editor of the HTML5 specification (some of these reasons are still valid, some of them are no longer relevant).

Here are a few examples:

  1. Well-formed XHTML, served as application/xhtml+xml
  2. Well-formed XHTML, served as text/html
  3. Not well-formed XHTML, served as application/xhtml+xml
  4. Not well-formed XHTML, served as text/html
  5. Well-formed HTML5, served as text/html

Note that the first does not work in IE. The second works in all browsers, but you are just wasting bytes; the browsers don't interpret namespaces properly, for example, so if you include XML in another namespace, it won't actually appear in another namespace in your scripts or CSS. The third will display a big error message, and the fourth will display just fine even though it's not well-formed XHTML (which demonstrates that the browsers are using their HTML parsers).

You can get the exact same effect as the text/html examples by using an HTML 4.01 doctype, or an HTML5 doctype, as demonstrated in the fifth example (this is also valid HTML 4.01 if you stick the HTML 4.01 doctype into it). If you use HTML 4.01 or HTML5, you can save a lot of space, and you won't be fooling yourself by sometimes processing the document as XHTML and sometimes processing it HTML.

Another reason to beware of serving XHTML as text/html is that it's processed differently depending on whether it's parsed by an HTML parser or an XML parser. For instance, a self-closing script tag, such as <script type="text/javascript" src="foo.js"/>, is valid in XHTML, and if parsed by an XML parser, will be parsed as an empty element. In HTML, however, this will be seen simply as a script opening tag, and it will "eat" the rest of the document, as the parser keeps parsing assuming it's inside a script until it finds a close tag. This is likely not what you want, and can trip you up if you sometimes treat a document as XML and sometimes treat it as HTML. Here's an example of this problem; these two documents display differently in Firefox, even though they have the same contents (Safari appears to treat them the same, so this extent of this problem varies between different browsers):

This is not the only difference between the HTML and XHTML parsers. In HTML, a <table> will have a <tbody> inserted into the DOM implicitly even if you don't have one in the source; in XHTML, if you don't specify a <tbody> explicitly, it will not be present in the DOM.

So, serving XHTML as text/html will make your code larger than it would be if you just used HTML 4.01 or HTML5, and it can lead to confusion if you sometimes process it as real XML and sometimes treat it as HTML.

Brian Campbell
@Brian - good answer :)
metal-gear-solid
so u mean if keep open any tag in XHTML then browser will not render or browser will render but validator will show error?
metal-gear-solid
If you're serving content as `text/html`, the browser will always parse it as HTML. This means that if you have a tag like `<p>`, with no close tag, the browser will parse it according to the rules of HTML, and close it at the appropriate time. If you serve the same as `application/xhtml+xml`, browsers that support XHTML will refuse to render the page (or will render the page up until the error). IE will just show the source code. A validator will give you an error.
Brian Campbell
but see this firefox is rendering witout closing tag http://jsbin.com/exuxi/2 only validation giving error.
metal-gear-solid
@Jitendra Right, that's being served as `text/html`, in which case the browser treats it as HTML, and missing close tags work OK. There's no advantage to use XHTML Strict if you serve as `text/html`; you're just wasting extra bytes, since the browser is treating it as HTML. As I said, if you serve it as `text/html`, that's how the browser interprets it, the `DOCTYPE` doesn't matter to the browser. If you serve it as `application/xhtml+xml`, the browser treats it as XTHML, and will display an error if it is invalid.
Brian Campbell
but now content is being served as a application/xhtml+xml http://jsbin.com/exuxi/3 and firefox still showing content of <p> without closing the tag and u wrote "If you serve the same as application/xhtml+xml, browsers that support XHTML will refuse to render the page"
metal-gear-solid
@Jitendra No, that's still being served as `text/html`; check the HTTP headers. The `<meta>` tag does not matter unless the HTTP headers are missing. See my examples for actually serving XHTML as `application/xhtml+xml`.
Brian Campbell
@Brian. Good answer, but I wish you were clearer about the cause of the YSOD. Validity is about matching a DTD (essentially, and leaving aside HTML5) and **Invalid** XHTML (served as application/xhtml+xml) will not necessarily cause a YSOD. The YSOD will only happen if the XHTML cannot be processed as XML, i.e. it is not XML well-formed, which is a much more serious problem.
Alohci
@Alohci You are right. I was typing that fast, and late at night. I've updated my answer to refer to well-formedness, not validity.
Brian Campbell
A: 

Yes: you're compliant to the standards when you send HTML 4.01 as text/html, but you're not when you send XHTML 1.0 strict as text/html. In fact, browsers believe you're sending them HTML 4.01 when you send them XHTML as HTML.

zneak