Your Ad Here

The Stokely XHTML MIME-TYPE TEST AND MIME TYPE 'SETTER' for WEB PAGES

Performing a MIME test for your browser: What Mime-Type is this page delivered in, based on your browser's 'type' and version? The code below will determine how best to deliver this XHTML page. Your browser can view this page in this MIME-type: application/xhtml+xml
Ok guys! So you are having problems with setting your web pages with the XHTML MIME type. How do you send the correct XHTML MIME-type to your viewers for your web pages, especially when browsers like Internet Explorer dont accept certain MIME types and crash and burn on them? Well, I am going to help you! Its actually a little complicated, especially explaining why and how XHTML is supposed to be delivered. There is allot of "crap" online about this and that and how to do this and what not to do. Im going to skip over all that and just tell you how to fix it, as I understand it. The solution below will allow you to tell your web server to send whats called an "html header" with the right XHTML MIME-type, which is stored invisibly inside every page (request/response) to and from the web server to the client. You cannot add markup to really influence that....it has to be set on the server for your web pages to deliver it correctly. After you do that, many people still have the problem with Internet Explorer, which doesnt understand the XHTML MIME type (list below). So, how do you first set the MIME type on the server, and second, how do you send IE one MIME and all the other browsers the XHTML MIME, given that they even support it? I will tell you....dont worry!....

Some people change the metatag to control MIMES, and so try and fix it that way. But it really doesnt do much as it is controlled in the header sent by the server itself when the page is requested by the browser (which you cannot see without special software or using "page info" boxes in browsers like Mozilla....go to "Tools/Page Info"). But, I usually include the MIME type via the metatags anyway to try and match what I want to send my viewers via the server's mime type in the html header. To be honest, Im not certain that the metatag version is necessary or does much. But some agents may use it and it also may be used by some XML applications so I would use it and make sure it matches what you send via the html header. Below is a sample of the new XHTML metatag using UNICODE standards, both which you should use in all your web pages (keeping in mind you still need to deliver the html header via the server, which I will get to next):
meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8"
Now, many of you are, like me, are trying to move to "Web Standards" using XHTML. Using this "transition" language you can use allot of stuff you learned in HTML, but you must clean up your act and do things, like close all tags, use lowercase elements, quote all attributes, and other stuff. Basically, this means your XHTML is also XML, which is cool, because your web page is then also a "data page", as I call it. Here is the problem....if you use the right DOCTYPE for XHTML (and one of its "flavors") it means the web browser must use that DOCTYPE and be compliant with its rules for settings tags and use it strictly, based on the MIME you send via the header. So when you use a browser like Mozilla, then deliver the correct html header and metatatags with the right MIME-types, you are doing it right and the browser will check your page strictly for comformance, which means correct validation against the MIME-type for XHTML, as controlled by the XHTML DOCTYPE. So, make sure you also have the right DOCTYPE for XHTML. Look online for one of these DOCTYPES or you can use the "loose" version, or "transitional" flavor below, which I like to. It allows you to use older HTML elements and attributes but also follow the XHTML standard:
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
Now remember, if you type code in the wrong place in your pages, so that the page is either not "well-formed" (broken tags) or does not validate (using bad html tags that are not supported for example), then the browser is supposed to basically either not render your page, or in other cases revert to a "quirksmode" so it can be rendered with support for sloppy code. Most people have decided to go with XHTML/XML which is picky and want to have it work as XHTML and risk that "break" feature, they so use very clean code in their web pages and deliver the pages as XHTML using the correct MIME type, which we are getting too.

Now, if you dont touch anything on the server and even if you use both the doctype and metatag for mime type above, guess what...it wont really affect the MIME type or how the browser display and renders your pages. By default, almost all servers deliver your page as "text/html" as a MIME type. Again, you can stick the metatag above in you page...but guess what....thats doesnt really change much. To do it right, you have to tell your web server to attach that same MIME code (not the same but similar actually) into the header of the page. To do so truly, you need to either access the web server software and map the extension for your page to application/xhtml+xml to .html, .html, .asp, .xhtml, etc. Or do it via a dynamic script. Below is a very cool script I rebuilt which if you are working on a Windows Server and IIs, will allow you to use ASP/ASP.NET code to detect if the browser supports the mime type, and if so, deliver the page using that MIME. It will tell the server to deliver your web page as application/xhtml+xml or XHTML and force the server to deliver this page as the correct MIME type. It will fit the MIME into the header for you, and BOOM!...you are delivering correct XHTML.

Do you think you are done....almost! One last issue. Remember, the major culprit with non-support for the application/xhtml+xml MIME is Internet Explorer (versions 3-6). It does not know how to display that MIME type for XHTML using the correct standardized MIME (application/xhtml+xml)....only the MIME for html or pure xml. So, to deliver the right MIME for IE, you need to deliver "text/html", or rather, you have to deliver the page with the same XHTML doctype, but with the MIME set to text/html. Basically you are delivering an XHTML page but using HTML MIMES for IE. Until IE 7 is released to fix this, thats what you must use. But the script below will take care of all that for you and deliver IE the old MIME and Mozilla and later versions of Opera the newer application/xhtml+xml version. Another very cool script from the STokely Script Library! (Yes, I take donations: mitchstokely@hotmail.com!)

(Be warned! Once you deliver your pages to Mozilla and Netscape with the correct MIME types which is application/xhtml+xml for XHTML, if any part of that page does not validate, you will get an ugly colored page indicating the error in those browser. BUT THIS IS A GOOD THING...as it means you are now delivering true XHTML (and thus XML ) and you have a chance to test its validity, conformance, and form and make corrections to get it cleaned up and working. That is how the browser are SUPPOSED to work (sorry, Internet Explorer!). Once its cleaned up, your pages will work and be readable, as this one should do in Mozilla, for you. :o))
USE THIS CODE IN ASP or ASP.NET to deliver the correct MIME Type to the browsers:


%
'PUT IN THE HEADER OF YOUR DOCUMENT...
'Setting the MIME Type with ASP
'The content type and the charset are specified via the Response object and the ServerVariables collection allows one to
'determine if its listed as an acceptable mime from the agent. 

dim printValue
printValue=""

If InStr(Request.ServerVariables("HTTP_ACCEPT"), "application/xhtml+xml") > 0 Then
Response.ContentType = "application/xhtml+xml"
printValue="application/xhtml+xml"
Response.Charset = "utf-8"
Else
Response.ContentType = "application/xhtml+xml"
Response.Charset = "utf-8"
printValue="text/html"
End If

%

%
'PUT THIS UNDER THE BODY TAG SO YOU DONT CRASH COMPLIANT XHTML BROWSERS
response.write "MIME-TYPE IS: " & printValue & "

" %