Simon Willison’s Weblog

Subscribe

Problems with XHTML content type.

30th May 2007

My answer to Problems with XHTML content type. on Ask MetaFilter

The first question you should be asking is why you need XHTML—if you don’t have a specific reason (the need for XML parsers to be able to consume your pages) you’re much better off with HTML 4.01 for now, and HTML 5 in probably a year or so.

As for serving up XHTML, you need to check the browser’s HTTP_ACCEPT header to decide whether or not to serve it (IE, including IE 7, can’t handle it at all and will offer to download it).

< ?phpbr>
if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
  header("Content-type: application/xhtml+xml");
}
else {
  header("Content-type: text/html");
}
?>< />
Be warned: there are a TON of gotchas involved in serving using the application/xhtml+xml content type, especially relating to JavaScript. This article from 2003 is still very relevant.

Then at 15:07:

Forgot to mention: the best reason to use XHTML is if you want to embed SVG or MathML in your pages. Again, that trick won’t work in any existing version of Internet Explorer.

Generally though it’s best avoided. I served my personal site in XHTML with the correct content type for several years; it was a nuisance. The XML error model is fundamentally unsuited to the Web.

One of the reasons HTML 5 is so interesting is that it attempts to specify an error model that is as close as possible to the way actual browsers work today.

This is Problems with XHTML content type. by Simon Willison, posted on 30th May 2007.

Next: Massive Dreamhost hack, WordPress not to blame

Previous: oxfordgeeks.net