ASP BrowserCap

BrowserType 对象识别浏览器能力、Browscap.ini、按能力降级。

Intermediate
Key PointsBrowserCap 读 Browscap.ini 把 User-Agent 映射成一组能力标志,用来决定输出什么。

基本用法

ASP
<%
    Dim bc
    Set bc = Server.CreateObject("MSWC.BrowserType")

    Response.Write "浏览器:" & bc.Browser & "<br>"
    Response.Write "版本:" & bc.Version & "<br>"
    Response.Write "平台:" & bc.Platform & "<br>"
    Response.Write "支持表格:" & bc.Tables & "<br>"
    Response.Write "支持框架:" & bc.Frames & "<br>"
    Response.Write "支持 Cookie:" & bc.Cookies & "<br>"
    Response.Write "支持 JS:" & bc.JavaScript & "<br>"
    Response.Write "支持 VBScript:" & bc.VBScript & "<br>"
    Response.Write "支持背景音乐:" & bc.BackgroundSounds

    Set bc = Nothing
%>
Output
浏览器:Chrome
版本:141
平台:WinNT
支持表格:True
支持框架:True
支持 Cookie:True
支持 JS:True
支持 VBScript:False
支持背景音乐:False

常用属性

PropertyMeaning
BrowserIE、Chrome、Firefox
Version主版本号字符串
MajorVer / MinorVer数字
PlatformWinNT、MacPPC
Frames是否支持 <frameset>
Tables是否支持 <table>
Cookies是否支持 Cookie
JavaScript是否支持 JavaScript
VBScript是否支持客户端 VBScript
ActiveXControls是否支持 ActiveX
Css是否支持样式表

Browscap.ini

INI
[IE 6.0]
browser=IE
version=6.0
majorver=6
minorver=0
platform=Win32
frames=TRUE
tables=TRUE
cookies=TRUE
javascript=TRUE
vbscript=TRUE
css=2

[Chrome]
browser=Chrome
version=141
platform=WinNT
frames=TRUE
tables=TRUE
cookies=TRUE
javascript=TRUE
vbscript=FALSE
css=3

[Default Browser Capability Settings]
browser=Default
version=0.0
frames=FALSE
tables=TRUE
cookies=FALSE
javascript=FALSE

Browscap.ini matches the User-Agent string. The older the file, the more new browsers fall through to Default and get treated as supporting nothing.

按能力降级

ASP
<%
    If bc.JavaScript Then
        Response.Write "<script>initApp();</script>"
    Else
        Response.Write "<noscript>请启用 JavaScript 以获得完整体验</noscript>"
    End If

    If bc.Cookies Then
        ' 用 Cookie 保存偏好
    Else
        ' 退回到 URL 参数或 Session
    End If
%>

Test capabilities, not browser names. Checking "is it IE" breaks the moment a new version ships; checking "does it support CSS3" stays valid.

BrowserCap is largely obsolete: every target browser supports tables, frames, cookies and JS. The problem it solved (IE4 vs Netscape) is gone, and keeping it only adds branches.

更现代的替代

JavaScript
// 客户端能力检测,比服务端猜 User-Agent 可靠得多
if ('IntersectionObserver' in window) {
  // 用新 API
} else {
  // 降级
}

Feature detection runs in the browser against real capabilities. User-Agent is self-reported, forgeable, and increasingly frozen by vendors (Chrome now pins the version in its UA).

Friends & Partners