Determining Client Browser Capabilities

[Previous] [Next]

In this solution, I want to illustrate how you can determine the browser requesting your ASP page and generate appropriate content based on its capabilities. If the browser supports COM controls, I want to send back an option to view the mortgage calculation in the Spreadsheet control, allowing users to see the content in full fidelity and directly edit the variables to perform a "what if" analysis. If the browser supports cascading style sheets (CSS), I want to use style attributes for formatting static results because style attributes provide more control over the table appearance and layout.

Those of you familiar with ASP programming might already know about the BrowserType object. For those who are not familiar with this object, I will briefly describe what it provides and how it works; for those who are familiar with it, I will show you how I am using it to determine what content to return.

The BrowserType object is supplied with IIS and is implemented in the MSWC library. It uses an INI file called Browscap.ini to determine what functionality a particular browser supports, such as COM controls. The object knows the type of client browser because all browsers by convention pass a string describing themselves when requesting information from a web server. This string contains the browser name, the full version number, and an indication of whether it is a beta or released version. The BrowserType object matches this string to one contained in the Browscap.ini file to determine which capabilities it should indicate the browser supports. Needless to say, the Browscap.ini file is the one that contains all the real information, and it is extremely important to keep this file up to date.

Using the BrowserType object is quite easy. The following code, taken from Default.asp, shows how I use it:

 ' Get information about the client browser Set m_BrowserInfo = Server.CreateObject("MSWC.BrowserType") On Error Resume Next If IsNumeric(m_BrowserInfo.majorver) Then     m_nMajorVer = CLng(m_BrowserInfo.majorver) Else     m_nMajorVer = 0 End If If IsNumeric(m_BrowserInfo.minorver) Then     m_nMinorVer = CLng(m_BrowserInfo.minorver) Else     m_nMinorVer = 0 End If m_fCOMCtls = CBool(m_BrowserInfo.ActiveXControls) m_sBrowserName = m_BrowserInfo.browser On Error Goto 0 ' Determine whether we should use CSS for formatting. ' Note: This assumes that if the browser's major ' version is 4 or greater, it supports CSS. That ' might not be the best test, and it would be better ' to include this in the Browscap.ini file for each browser. m_fUseCSS = (m_nMajorVer >= 4) 

As mentioned in the code comment above, there does not seem to be a property for determining whether the client browser supports CSS. In place of an explicit property, I check the major version number of the browser and if it is equal to or greater than 4, I assume that the browser does support CSS. Although Internet Explorer 3 supported some parts of the CSS standard, in this solution I assume that I should use CSS formatting attributes only for version 4 or later.

So Where Do I Find the Current Browscap.ini File?

When I started to write the code for this chapter, I quickly built a page that would echo back to the client browser a True or False value indicating whether the page thought the browser supported COM controls. I started Internet Explorer version 5.0, hit the page, and promptly got back False. "What do you mean Internet Explorer 5.0 doesn't support COM controls?" I mused.

Of course, my problem was that I had installed IIS 4, which shipped long before Internet Explorer 5 released, and therefore, the Browscap.ini file on my machine had no information for Internet Explorer 5. So I wondered, "How do I get the most current version of this file?" I went to http://www.microsoft.com, went to the search page, and typed browscap.ini. Amazingly, the first search result was titled "Where can I find the latest browscap.ini file?" Following the link will eventually lead you to the cyScape.com site, where you can download the latest version of this file for free after viewing lots of advertising for their product that automatically downloads new versions for you.

If you run the Loan Calculation solution using Internet Explorer 5 and you have not downloaded the newest Browscap.ini file, you will notice that you do not have the option to view the results in an interactive Spreadsheet control. Update the file and you will see the option. You can also manually append Interactive=on to the end of the URL generated by the form to force the ASP script to return an interactive page.



Programming Microsoft Office 2000 Web Components
Programming Microsoft Office 2000 Web Components (Microsoft Progamming Series)
ISBN: 073560794X
EAN: 2147483647
Year: 1999
Pages: 111
Authors: Dave Stearns

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net