I am quietly at work planning out my re-design for BluHelix Studios. While one of my goals is to migrate to a system bearing a resemblance to the header/footer system I created for the Berglund Center sites, I have also been meaning to do something else.
XHTML.
What? Hold it! But you thought BluHelix Studios already used XHTML! Isn’t there a DOCTYPE right now for XHTML 1.0 Transitional?
Well, yes and no. See, there is a dirty little secret to many of the WWW’s XHTML web sites: they are really serving your browsers malformed, error-inducing HTML documents. The errors, of course, are simply XHTML tags that the HTML parser is choking on because they are not part of the HTML specification. You see, the XHTML DOCTYPE is only used when validated a page (and for setting IE in compliance mode), it is really the MIME type of application/xhtml+xml and the many variants that define an XHTML document.
The reason why these “real” XHTML documents are so rarely served is because the most popular browser in the world is not equipped to parse XHTML documents. IE7 is at least slightly better. Previous versions wouldn’t have a clue what to do with the non text/html MIME type and would spawn a download prompt as if you were trying to download a ZIP, EXE, etc. IE7 will successfully display the page – but using its HTML parser.
Other popular browsers like Firefox, Opera, and Safari are equipped with XML parsers.
So, at first it might seem that serving “real” XHTML documents means alienating 85%-90% of your user base. Luckily there is a technique to, literally, get both worlds. The solution: content negotiation.
Content negotiation is the process whereby the user agent (your browser in most cases) communicates to the server informing it of the technologies it understands and even which ones it prefers. The server responds by sending it content in the format the user agent prefers. This is actually a really cool idea, and if you read up on XHTML 2.0 it appears that this kind of handshaking will eventually be integrated into the technology itself and be very trivial to do. But for now we must resort to scripting.
There are two approaches to this: setting up those super-rad Apache/IIS URL rewriting to serve the content most appropriate for the requesting user agent. Or, this functionality can be done through server-side scripts. I chose the latter and coded up a PHP class to preform XHTML/HTML content negotiating. This script is pretty easy to use and preforms about a dozen regular expressions on the output buffer in an attempt to transform markup to XHTML and HTML, depending on the needs of the requesting user agent.
The usage is simple. Just instantiate the class by giving the constructor the type of document you would prefer to transmit, then add style sheets to the class, call the pro log method just before content output, call the style sheet method inside the head tag, and call the finalize method at the bottom of the page. Done.
I commented both the script and test file for your convenience. You may download the script here.
This is not the only application of content negotiation. Another common use is to chose between PNG and GIF images, depending on the capabilities of the user agent.
Unknown8063 Inside BluHelix, Studio Announcement