XHTML MimeType Negotiation For ASP

Audience Level

Intermediate to advanced

Introduction

This JScript ASP function determines if a client browser or internet device supports the "application/xhtml+xml" mimetype by analysing the headers sent by the client. The following function returns a boolean output that should be tested before setting the relevant return content type — see the example fragment included.

Source Code

/*
Function: getXHTMLBrowserStatus()
Description:
Returns:
History:
20040706 1113BST    v1      Andrew Urquhart     Ported to ASP JScript from PHP by Toby Inkster
*/

function getXHTMLBrowserStatus() {
    try {
        var objAccept       = Request.ServerVariables("HTTP_ACCEPT");
        var objUserAgent    = Request.ServerVariables("HTTP_USER_AGENT");
        var strAccept       = objAccept.Count() ? objAccept.Item(1).toLowerCase() : "";
        var strUserAgent    = objUserAgent.Count() ? objUserAgent.Item(1).toLowerCase() : "";

        // NETSCAPE 6 CAN'T PROPERLY HANDLE XHTML
        if (strUserAgent.indexOf("Netscape6") >= 0) {
            return false;
        }

        // WGET CAN (WELL, IT DOESN'T CARE!), BUT DOESN'T LIST IT
        if (strUserAgent.indexOf("Wget") >= 0) {
            return true;
        }

        // ANY BROWSERS THAT DON'T SPECIFICIALLY LIST XHTML, USE HTML
        if (strAccept.indexOf("application/xhtml+xml") < 0) {
            return false;
        }

        // OTHERWISE WE NEED TO COMPARE 'Q' VALUES.
        else {
            var fltXHTML_q  = 1;
            var fltHTML_q   = 1;

            if (strAccept.match(/application\/xhtml\+xml\s*;[^,]*\bq\s*=\s*([\d\.]+)/)) {
                fltXHTML_q = parseFloat(RegExp.$1);
            }

            if (strAccept.match(/text\/html\s*;[^,]*\bq\s*=\s*([\d\.]+)/) || strAccept.match(/text\/\*\s*;[^,]*\bq\s*=\s*([\d\.]+)/) || strAccept.match(/\*\/\*\s*;[^,]*\bq\s*=\s*([\d\.]+)/)) {
                fltHTML_q = parseFloat(RegExp.$1);
            }
            return (fltXHTML_q >= fltHTML_q);
        }

    }
    catch (err) {
        throw new Error(err.number, "Function getXHTMLBrowserStatus() failed with message=\r\n" + err.description);
    }
}

Download

Download the source directly.

Example Usage

// SET MIME TYPE AND XML DECLARATION WHERE APPLICABLE
if (getXHTMLBrowserStatus()) {
    Response.ContentType = "application/xhtml+xml";
    Response.Write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
}
else {
    Response.ContentType = "text/html";
}

// NOW WRITE DOCTYPE AND CONTINUE PROCESSING DOCUMENT ETC.
Response.Write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");

Credits

This function has been ported to ASP JScript from a PHP function by Toby Inkster/Bertilo Wennergren as seen on USENET in the following thread: function goodbrowser() (2004-06-30 11:06:16 PST).

Advertisement

Feedback

Voting Panel
Is this useful?
or
Did you find any bugs?
or
Did it solve your programming problem?
or
Rate this script: (0=poor, 5=very good)
Answers are anonymous, only the combined totals are stored. Uses cookies.