One of the difficulties in designing web pages for the Internet is your users may be using a number of different browsers. The fierce competition between Netscape and Microsoft has led to differing “standards.” Internet Explorer, for example, supports client-side VBScript and ActiveX controls. Netscape, however, supports JavaScript as the only client-side scripting language, and ActiveX controls can only be used with a plug-in. Similarly, both Internet Explorer and Netscape have their own protocol and extensions for Dynamic HTML and cascading style sheets.
While Internet Explorer and Netscape are by far the two most popular browsers on the market, there are many other browsing options available. If users may visit your site through the WebTV browser, AOL’s custom browser, lynx (a text-based Unix browser), Opera, or any other non-mainstream browser, it is important that you ensure that your site still looks pleasant and is easy to use. For example, if you had a set of web pages that utilized DHTML and a user visited your site using a browser that didn’t support DHTML (older versions of Internet Explorer and Netscape, lynx, etc.), you would want to redirect the user to a set of pages that accomplished the same tasks but refrained from using DHTML.
As mentioned in Chapter 7, Microsoft provides a
free COM component to accomplish this task:
the Browser Capabilities component. This
component has a couple of drawbacks, though. For starters, to use the
Browser Capabilities component, a browser capabilities file is needed
and must be updated each time a new browser comes to market or a new
browser version is released. This file, named
browscap.ini
,
contains information about all of the popular browser types and each
browser’s capabilities.
For the latest version of browscap.ini
, visit
http://www.asptracker.com.
Another disadvantage of the Browser Capabilities component is it only reports on a small set of available browser properties. Furthermore, the Browser Capabilities component only indicates whether or not the user’s browser supports a certain property. For example, Internet Explorer 5.0 supports client-side JavaScript; however, users can easily disable JavaScript support. The Browser Capabilities component, therefore, would indicate that a visitor who was using Internet Explorer 5.0 supported client-side JavaScript, even if the user had disabled JavaScript from their browser.
To compensate for the disadvantages of the Browser Capabilities component, you can use BrowserHawk, a third-party component from cyScape, Inc. Like the Browser Capabilities component, BrowserHawk maintains a list of potential browser types and versions and their various capabilities. Unlike the Browser Capabilities component, however, BrowserHawk will automatically check for and download updates to this list of the various browsers’ capabilities. Furthermore, BrowserHawk contains a much more thorough list of browser capabilities than the Browser Capabilities component.
A free 30-day evaluation copy of BrowserHawk can be downloaded from cyScape’s web site at http://www.cyscape.com/products/bhawk/start.asp. At the time this book was published, BrowserHawk offered Standard, Professional, and Enterprise editions costing $119, $274, and $489 USD, respectively.
Once BrowserHawk is installed, you can start using it in your ASP pages to detect your visitor’s browser information! BrowserHawk contains an extremely lengthy set of properties for each possible browser type and version. Table 8.3 contains a list of some of the more useful properties. A complete list can be found online at http://www.cyscape.com/showbrow.asp.
Table 8-3. BrowserHawk Indicates the Properties Your Users’s Browsers Support
Property |
Description |
---|---|
ActiveXControls |
Boolean; indicates whether or not the user’s browser supports ActiveX controls. |
Cookies |
Boolean; indicates whether or not the user’s browser supports cookies. |
CookiesEnabled |
Boolean; indicates whether or not the visitor has cookies enabled.
For browsers that accept cookies, this property checks to ensure the
user has cookies enabled. (For a browser that supports cookies but
has disabled cookies, the Cookies property would return
|
DHTML |
Boolean; indicates whether or not the user’s browser supports DHTML. |
Frames |
Boolean; indicates whether or not the visitor’s browser supports frames. |
Height |
Returns the height of the visitor’s screen resolution. For example, a visitor who was viewing your site at 800x600 would have a Height property of 600. |
IPAddr |
Returns the IP address of the visitor. |
JavaApplets |
Boolean; indicates whether or not the user’s browser supports Java applets. |
JavaEnabled |
Boolean; similar to the CookiesEnabled property, this property indicates whether or not the visitor has Java applets enabled. |
JavaScript |
Boolean; indicates whether or not the user’s browser supports client-side JavaScript. |
JavaScriptEnabled |
Boolean; similar to the CookiesEnabled property, this property indicates whether or not the visitor has client-side JavaScript enabled. |
StyleSheets |
Boolean; indicates whether or not the user’s browser supports cascading style sheets (CSS). |
VBScript |
Boolean; indicates whether or not the user’s browser supports client-side VBScript code. |
Version |
Returns the version of the browser. |
Width |
Returns the width of the visitor’s screen resolution. For example, a visitor who was viewing your site at 800x600 would have a Width property of 800. |
XML |
Boolean; indicates whether or not the user’s browser supports XML. |
The Enabled properties—C
ookiesEnabled,
JavaEnabled, and JavaScriptEnabled — only work with the
Professional or Enterprise editions of BrowserHawk.
Once you have run through the BrowserHawk installation process, the BrowserHawk DLL will be registered on your machine. You are now ready to start using BrowserHawk in your ASP pages! To create an instance of the BrowserHawk component, use the following lines of code:
DimobjBrowserHawkInstance
SetobjBrowserHawkInstance
= Server.CreateObject("cyScape.browserObj
")
Before reading the Height, Width, JavaScriptEnabled, or JavaEnabled properties, you must call the GetExtProperties method. This method sends a blank page to the client to assess which of these properties, if any, are disabled. Likewise, before reading the CookiesEnabled property, call the CookieDetector method. The BrowserHawk documentation details what properties need to have a method called before being read.
Example 8.2 demonstrates how to use the BrowserHawk component to determine what properties your visitor supports. Note that the GetExtProperties and CookieDetector methods are called prior to listing the capabilities of the user’s browser.
Example 8-2. BrowserHawk Determines What Properties Are Supported by a Visitor’s Browser
<% @LANGUAGE="VBSCRIPT" %> <% Option Explicit %> <% Dim objBHawk Set objBHawk = Server.CreateObject("cyScape.browserObj") 'Get the extended properties objBHawk.GetExtProperties 'Call the cookie detector objBHawk.CookieDetector %> <HTML> <BODY> <H1>Fun Facts About Your Browser!</H1> Your resolution: <%=objBHawk.Width%>x<%=objBHawk.Height%><BR> Cookies Enabled: <%=objBHawk.CookiesEnabled%><BR> Support VBScript: <%=objBHawk.VBScript%><BR> Support XML: <%=objBHawk.XML%><BR> JavaScript Enabled: <%=objBHawk.JavaScriptEnabled%><BR> Java Applets Enabled: <%=objBHawk.JavaEnabled%> </BODY> </HTML> <% Set objBHawk = Nothing 'Clean up! %>
When the ASP page presented in Example 8.2 is visited through a browser, the output will differ on depending what browser is used to visit the page! I visited this page with Microsoft’s Internet Explorer 5.0; a screenshot can be seen in Figure 8.3.
Web developers face a real dilemma when trying to create easy-to-use web sites. While many new technologies have arisen to help make the web experience more interactive (Macromedia Flash, for example, or Dynamic HTML), these add-ons are not standard across all browsers and all versions. Therefore, many web developers choose to create two sets of web pages. One set utilizes the advanced features—such as cascading style sheets, Java applets, and XML — supported by the more recent browsers. The second set of pages provides similar functionality but uses the older technologies—vanilla HTML and simple client-side JavaScript commands.
When a visitor first arrives at the site, he is redirected to the
appropriate set of pages, depending upon his browser. For example,
the following snippet of code could be used on the start page
( /default.asp
) to send the user to a certain
set of web pages based upon his browser’s support of DHTML:
<% @LANGUAGE="VBSCRIPT" %> <% Option Explicit %> <% Dim objBHawk Set objBHawk = Server.CreateObject("cyScape.browserObj") 'Redirect the user based on the browser's ability to support DHTML If objBHawk.DHTML then 'Supports DHTML Response.Redirect "/DHTMLSupport/default.asp" Else 'Does not support DHTML Response.Redirect "/NonDHTML/default.asp" End If %>
Then, for each ASP page in the /DHTMLSupport
directory, you’d want to use a server-side include to add the
following ASP code:
<% Dim objBHawk Set objBHawk = Server.CreateObject("cyScape.browserObj") 'Redirect the user to the NonDHTML section if they don't support DHTML If Not objBHawk.DHTML then 'Does not support DHTML Response.Redirect Replace(Request.ServerVariables("SCRIPT_NAME"), _ "DHTMLSupport", "NonDHTML") End If %>
This snippet of code would redirect the user to the proper page in
the NonDHTML directory if the browser
didn’t support DHTML. That way, if some user entered the URL
http://www.yoursite.com/DHTMLSupport/SomePage.asp
into their non-DHTML supporting browser, they would be automatically
redirected to http://www.yoursite.com/NonDHTML/SomePage.asp
.
Similarly, in all of the ASP pages in the
/NonDHTML
directory, you’d want to add a
similar block of ASP code to the one above that would check to see if
DHTML was supported, and if so, would redirect the user to the
appropriate page in the /DHTMLSupport
directory.
3.141.31.209