ตรวจสอบคุณสมบัติบราวเซอร์ง่าย ๆ ด้วย Cyscape BrowserHawk 2000

Often you will want to send content to the client only if you know their browser can support it. Likewise you may be aware of specific problems that certain browsers have when visiting your site. In this lesson we demonstrate how to detect specific browser types and versions from your scripts so you can handle these differences seamlessly.

We start this lesson with a simple demonstration of how to detect older versions of AOL browsers. This is accomplished quite easily:

      <%
      set bh = server.createobject("cyScape.browserObj")
      if bh.AOL=True AND bh.Majorver <=3 then
         response.redirect("olderAolBrowsers.asp")
      end if
      %>
      <html><body> </body></html>
      
To detect the browser type and version, you first create an instance of the BrowserHawk ActiveX component using the Server.CreateObject statement. Then you simply check the properties that you are interested in. In this case we look to see if the AOL property is True, and if it is, we check to see if the major version number is 3 or less. If so we redirect the user to an alternate page.

This technique also comes in handy if you have created pages designed to work only in IE and Netscape, version 4 or greater. This example shows how to use BrowserHawk to redirect the user to an alternate page if not IE or NN version 4 or greater.

      <%
      set bh = server.createobject("cyScape.browserObj")
      v = bh.version
      b = bh.browser
      if (v>=4) and (b="IE" or b="Netscape") then
      else
         response.redirect("UpdateBrowser.asp")
      end if
      set bh = nothing
      %>
      <html><body> </body></html>
      
Remember that the response.redirect code should go before any of the HTML content on the page. You can keep this check script in a separate file, and include it at the top of all pages within an application.

Another example is the title attribute for HTML elements which, currently, is only supported by IE 4 or greater. While the title attribute degrades fine in other browsers, it is sometimes desirable to limit the amount of superfluous code being sent to the browser... regardless of how insignificant it may seem. This example will show you a quick and easy way to let BrowserHawk decide for you whether or not to use that title attribute.

Here is an example of the title attribute. Since you are using IE4 or greater, if you hold your mouse over the following link for a few seconds, you will see a tooltip, similar to the kind you see when you hold your mouse over a button on an application's toolbar. This allows you to include a lot more information in a small space, such as a navigation frame or a slim table cell.

Using two methods of BrowserHawk, we can quickly and easily check if the visitor is using a browser which supports the title element (namely, that it is Internet Explorer AND that it is version 4 or greater). Here is what the code looks like:

      <%
      set bh = server.createobject("cyScape.browserObj")
      if bh.version>=4 and bh.browser="IE" then
      %>
        <a href='x.asp' title=' This is the alternate text. '>
      <% else %>
        <a href='x.asp'>  
      <%
      end if
      set bh = nothing
      %>
      Hover here</a>
      
This logic could be used for any feature that you know is only supported by certain browser versions. Sometimes it is worth it to prevent the waste of bandwidth caused by adding elements and features to a page when they can't be viewed anyway. But more likely then not, you'll use this approach to avoid inconsistencies with layout and scripting across browsers.

Download the ASP files from this lesson.
Demo
Documentation

 

  © 2000, Integrated World Tech Co.,Ltd , All Rights Reserved. Thairegister is a service mark of In and Intergrated World Tech Co.,Ltd. Additional copyright and trademark information.