Jump to content
xisto Community
Sign in to follow this  
iGuest

Detecting XHTML + XML In Browsers Can it be improved?

Recommended Posts

I have written a script to detect if the mime-type application/xhtml+xml is supported in browsers, yet some browsers that do support this mime-type have to be tested manually so I was wondering whether there was a better method. Something I would rather do is intercept the browser before it decides to download the page instead, is this possible?

 

Here's the script I wrote, which detects strings from the User Agent, it's just a small snippet of it:

 

<?php// function to test for application/xhtml+xml mime-type supportfunction CheckXHTML(){  // For Robots, Spiders, Validators and other User Agents without displaying any mime-type  if(!isset($_SERVER['HTTP_ACCEPT']))    return TRUE;  // For Browsers that show the mime-type supported  elseif(strpos(strtolower($_SERVER['HTTP_ACCEPT']), 'xhtml+xml'))  // Browsers that support it here: Amaya, Epiphany, Firefox, Galeon, Opera     return TRUE;    // Created an array incase more browsers need to be added.    $UA = array('konqueror');  // Browsers that were tested and found to support it but fails the above: Konqueror   foreach($UA as $value)  {    if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), $value))      return TRUE;  }  // Those User Agents that fail the above   return FALSE;  // Browsers that do not support it: IE, Lynx}  if(CheckXHTML())  echo 'Supported';else  // Shows information about the User Agent if not supported.  echo 'Not Supported<br />'.$_SERVER['HTTP_ACCEPT'].'<br />'.$_SERVER['HTTP_USER_AGENT'];?>

The main reason for doing this, is because when creating pages in XHTML 1.1, we also have to make sure the header('Content: application/xhtml+xml; charset=utf-8'); is sent, but some browsers do not support this, so far only IE is my concern, but it can still render it using text/html, which is not the correct way to handle this, but as long as the document is still conforming to HTML 4.01 or XHTML 1.0 it should be safe to use text/html and still have the page displayed correctly by that browser, but sending text/html for browsers that support the XHTML mime-type will have slight problems.

 

Is there a better method than above? Especially if we can intercept the browser before it wants to download the file. e.g. If the browser doesn't support application/xhtml+xml like IE, they would display the download box to download the page, once downloaded and opened, you'd see it's just the XHTML code which can be displayed in IE by giving it an html extension and opening it with IE.

 

Here's a test page you could test out, which does not use the detection method but is strictly an XHTML 1.1 document, I also added some other things to the header so that the document would be handled better if cached, quite handy to have.

 

<?php 
  // Send XHTML mime-type
	header('Content-Type: application/xhtml+xml; charset=utf-8');
	// Send Last-Modified, ETag and Expires header, never set for dynamic scripts
  header('Last-Modified: '.gmdate('D, d M Y H:i:s T',getlastmod()));
  header('ETag: "'.dechex(getmyinode()).'-'.dechex(filesize($_SERVER['SCRIPT_FILENAME'])).'-'.dechex(getlastmod()).'"');
  // Expires: time() + days_till_expiration * hours_in_a_day * minutes_in_an_hour * seconds_in_a_minute
  header('Expires: '.gmdate('D, d M Y H:i:s T',time()+ 1 * 24 * 60 * 60));
  // XML preprocessing string, split in case short_tags are enabled. 
  echo '<'.'?xml version="1.0" encoding="utf-8"?'.'>'."\n";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://forums.xisto.com/no_longer_exists/;
<html xmlns="http://www.w3.org/1999/xhtml/; xml:lang="en">
  <head>
    <title>XHTML 1.1 Test Page</title>
  </head>
  <body>
      <p>
        This is just a test. If your browser displays this and does not ask for this page to be downloaded, your browser supports 
        <acronym title="eXtensible HyperText Markup Language">XHTML</acronym>.
      </p>
  </body>
</html>

 

That's it, cheers,

 

 

MC

Share this post


Link to post
Share on other sites

Is there a better method than above?  Especially if we can intercept the browser before it wants to download the file. e.g. If the browser doesn't support application/xhtml+xml like IE, they would display the download box to download the page, once downloaded and opened, you'd see it's just the XHTML code which can be displayed in IE by giving it an html extension and opening it with IE.

1064332505[/snapback]


I can't think of how could it be possible. As far as I'm concerned, there is no way for us to detect whether IE is going to display a download dialogue or not.

 

And anyways, I see no reason to change that function. It already does what it needs to do. I personally have treated IE issues by detecting if browser just identifies itself as IE and then go for the "works in IE" code. The only exception I do take in account is Opera (because if its relatively big popularity) and any other browser masking itself as IE just has to pay for it :huh:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.