
how-to block ads
|
  JAAulde yum yum yum yum yum Premium,MVM join:2001-05-09 Hagerstown, MD
| Stickler for standards? Declare your XML!
I have gotten tired of avoiding certain standards because of IE (and a few others) and am now declaring my XML and sending the right MIME types on my XHTML pages. This causes problems in browsers who do not send the proper HTTP ACCEPT header in their requests (cough, IE, cough) so you need to sniff out and parse the ACCEPT header.
I found code online to do this, and modified it to compact it a bit. Basically, you check to see if the right MIME type (application/xhtml+xml) appears in the ACCEPT header, and if it does, parse it out to check versions. If all looks right, you serve up the true XML as follows: *Content type header of application/xhtml+xml *XML declaration *XHTML 1.1 Strict DOCTYPE *lang attribute removed from root HTML element
Otherwise, you send *Content type header of text/html *XHTML 1.0 Strict DOCTYPE *lang attribute present in root HTML element *META tag with content-type and charset
An example of the output can be seen in a new layout I am working on: »www.jaaulde.com/test_bed/keys Check it out in Fx and IE and notice the differences.
The code I am using for this is here: php code:<?php $charset = "utf-8"; //Start with a base mime and move up to xml if acceptable $mime = "text/html"; if(stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml")|| stristr($_SERVER["HTTP_USER_AGENT"],"WDG_Validator")|| stristr($_SERVER["HTTP_USER_AGENT"],"W3C_Validator")|| stristr($_SERVER["HTTP_USER_AGENT"],"W3C_CSS_Validator") ) { //If 'q' is present, xml-q should be greater than or equal to html-q for mime of application/xhtml+xml if(preg_match("/application\/xhtml\+xml;q=0(\.[1-9]+)/i",$_SERVER["HTTP_ACCEPT"],$matches)) { $xhtml_q = $matches[1]; if(preg_match("/text\/html;q=0(\.[1-9]+)/i",$_SERVER["HTTP_ACCEPT"],$matches)) { $html_q = $matches[1]; if($xhtml_q >= $html_q) { $mime = "application/xhtml+xml"; } } } else { $mime = "application/xhtml+xml"; } } if($mime == "application/xhtml+xml") { $xmlDec = '<'.'?xml version="1.0" encoding="'.$charset.'"?'.'>'; $DOCTYPE = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'; $rootEleAttrs = ' xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"'; $contentTypeMeta = ''; } else { $xmlDec = ''; $DOCTYPE = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'; $rootEleAttrs = ' xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"'; $contentTypeMeta = "\t".'<meta http-equiv="content-type" content="text/html; charset=utf-8" />'; } header("Content-Type: $mime;charset=$charset"); header("Vary: Accept");
//OUTPUTTING PROPER SETTINGS: if(!empty($xmlDec)){echo $xmlDec."\n";} if(!empty($DOCTYPE)){echo$DOCTYPE."\n";} ?><html<?php if(!empty($rootEleAttrs)){echo $rootEleAttrs;}?>> <head> <?php if(!empty($contentTypeMeta)){echo $contentTypeMeta."\n";} ?>
After this chunk of PHP, you simply continue along with your TITLE element and other things in your HEAD, close your HEAD and start to work on your BODY.
-- "There's a war going on insdie me--the good fightin' against the evil. But I thank God for redemption."
--Johnny Cash
| |   Gwellin Premium join:2004-05-31 Regina, SK
·Access Communicati..
| That's a great thing to have, good find. I have thought about doing that sort of thing but I never wanted to spend the time to figure it out, both what is needed and what IE supports.
Lately my pages tend to have the <?xml declaration and if it really messes Explorer up then I drop the <?xml after the <!DOCTYPE. This is not the proper way to do it (still validates though, for now) but if IE sees a <!DOCTYPE on the first line then it doesn't use quirks mode.
I may start using this script. Thanks. -- Here to help all those in need, whenever I can. | |   JAAulde yum yum yum yum yum Premium,MVM join:2001-05-09 Hagerstown, MD
| reply to JAAulde Yeah, the real issue is that by standard XML requires the declaration on line one and IE wants a DOCTYPE on line one.
This little lesson has motivated me to write version 2 of my header request page as well. I have never sent an Accept header that matches the UA I claim with it, so a lot of things stay the same even though I claim different browsers in the UA string. Version 2 is not online yet, though, so you can't use it to test difference in the Accept header yet. -- "There's a war going on insdie me--the good fightin' against the evil. But I thank God for redemption."
--Johnny Cash
| |
|