JavaScript判断浏览器类型及版本

你知道世界上有多少种浏览器吗?除了我们熟知的IE, Firefox, Opera, Safari四大浏览器之外,世界上还有近百种浏览器。

       几天前,浏览器家族有刚诞生了一位小王子,就是Google推出的Chrome浏览器。由于Chrome出生名门,尽管他还是个小家伙,没有人敢小看他。以后,咱们常说浏览器的“四大才子”就得改称为“五朵金花”了。

       在网站前端开发中,浏览器兼容性问题本已让我们手忙脚乱,Chrome的出世不知道又要给我们添多少乱子。浏览器兼容性是前端开发框架要解决的第一个问题,要解决兼容性问题就得首先准确判断出浏览器的类型及其版本。

       JavaScript是前端开发的主要语言,我们可以通过编写JavaScript程序来判断浏览器的类型及版本。JavaScript判断浏览器类型一般有两种办法,一种是根据各种浏览器独有的属性来分辨,另一种是通过分析浏览器的userAgent属性来判断的。在许多情况下,值判断出浏览器类型之后,还需判断浏览器版本才能处理兼容性问题,而判断浏览器的版本一般只能通过分析浏览器的userAgent才能知道。

       我们先来分析一下各种浏览器的特征及其userAgent。

       IE

      只有IE支持创建ActiveX控件,因此她有一个其他浏览器没有的东西,就是ActiveXObject函数。只要判断window对象存在ActiveXObject函数,就可以明确判断出当前浏览器是IE。而IE各个版本典型的userAgent如下: 

        Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)
        Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)
        Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
        Mozilla/4.0 (compatible; MSIE 5.0; Windows NT)

      其中,版本号是MSIE之后的数字。

       Firefox

       Firefox中的DOM元素都有一个getBoxObjectFor函数,用来获取该DOM元素的位置和大小(IE对应的中是getBoundingClientRect函数)。这是Firefox独有的,判断它即可知道是当前浏览器是Firefox。Firefox几个版本的userAgent大致如下:

        Mozilla/5.0 (Windows; U; Windows NT 5.2) Gecko/2008070208 Firefox/3.0.1
        Mozilla/5.0 (Windows; U; Windows NT 5.1) Gecko/20070309 Firefox/2.0.0.3
        Mozilla/5.0 (Windows; U; Windows NT 5.1) Gecko/20070803 Firefox/1.5.0.12
      其中,版本号是Firefox之后的数字。

       Opera

       Opera提供了专门的浏览器标志,就是window.opera属性。Opera典型的userAgent如下: 

        Opera/9.27 (Windows NT 5.2; U; zh-cn)
        Opera/8.0 (Macintosh; PPC Mac OS X; U; en)
        Mozilla/5.0 (Macintosh; PPC Mac OS X; U; en) Opera 8.0

      其中,版本号是靠近Opera的数字。

       Safari

       Safari浏览器中有一个其他浏览器没有的openDatabase函数,可做为判断Safari的标志。Safari典型的userAgent如下:

        Mozilla/5.0 (Windows; U; Windows NT 5.2) AppleWebKit/525.13 (KHTML, like Gecko) Version/3.1 Safari/525.13
        Mozilla/5.0 (iPhone; U; CPU like Mac OS X) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A93 Safari/419.3 

      其版本号是Version之后的数字。

      Chrome

      Chrome有一个MessageEvent函数,但Firefox也有。不过,好在Chrome并没有Firefox的getBoxObjectFor函数,根据这个条件还是可以准确判断出Chrome浏览器的。目前,Chrome的userAgent是: 

        Mozilla/5.0 (Windows; U; Windows NT 5.2) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13

      其中,版本号在Chrome只后的数字。

      有趣的是,Chrome的userAgent还包含了Safari的特征,也许这就是Chrome可以运行所有Apple浏览器应用的基础吧。

      只要了解了以上信息,我们就可以根基这些特征来判断浏览器类型及其版本了。我们会将判断的结果保存在Sys名字空间中,成为前端框架的基本标志信息,供今后的程序来读取。如果判断出谋种浏览器,Sys名字空间将有一个该浏览器名称的属性,其值为该浏览器的版本号。例如,如果判断出IE 7.0,则Sys.ie的值为7.0;如果判断出Firefox 3.0,则Sys.firefox的值为3.0。下面是判断浏览器的代码:

<mce:script type="text/javascript"><!--
        
var Sys = {};
        
var ua = navigator.userAgent.toLowerCase();
        
if (window.ActiveXObject)
            Sys.ie 
= ua.match(/msie ([\d.]+)/)[1]
        
else if (document.getBoxObjectFor)
            Sys.firefox 
= ua.match(/firefox\/([\d.]+)/)[1]
        
else if (window.MessageEvent && !document.getBoxObjectFor)
            Sys.chrome 
= ua.match(/chrome\/([\d.]+)/)[1]
        
else if (window.opera)
            Sys.opera 
= ua.match(/opera.([\d.]+)/)[1]
        
else if (window.openDatabase)
            Sys.safari 
= ua.match(/version\/([\d.]+)/)[1];
        
        
//以下进行测试
        if(Sys.ie) document.write('IE: '+Sys.ie);
        
if(Sys.firefox) document.write('Firefox: '+Sys.firefox);
        
if(Sys.chrome) document.write('Chrome: '+Sys.chrome);
        
if(Sys.opera) document.write('Opera: '+Sys.opera);
        
if(Sys.safari) document.write('Safari: '+Sys.safari);
    
// --></mce:script>

我们把对IE的判断放在第一,因为IE的用户最多,其次是判断Firefox。按使用者多少的顺序来判断浏览器类型,可以提高判断效率,少做无用功。之所以将Chrome放在第三判断,是因为我们预测Chrome很快会成为市场占有率第三的浏览器。其中,在分析浏览器版本时,用到了正则表达式来析取其中的版本信息。

      如果你的JavaScript玩得很高,你还可以将前面的判断代码写成这样:

 <mce:script type="text/javascript"><!--
        
var Sys = {};
        
var ua = navigator.userAgent.toLowerCase();
        window.ActiveXObject 
? Sys.ie = ua.match(/msie ([\d.]+)/)[1] :
        document.getBoxObjectFor 
? Sys.firefox = ua.match(/firefox\/([\d.]+)/)[1] :
        window.MessageEvent 
&& !document.getBoxObjectFor ? Sys.chrome = ua.match(/chrome\/([\d.]+)/)[1] :
        window.opera 
? Sys.opera = ua.match(/opera.([\d.]+)/)[1] :
        window.openDatabase 
? Sys.safari = ua.match(/version\/([\d.]+)/)[1] : 0;
        
        
//以下进行测试
        if(Sys.ie) document.write('IE: '+Sys.ie);
        
if(Sys.firefox) document.write('Firefox: '+Sys.firefox);
        
if(Sys.chrome) document.write('Chrome: '+Sys.chrome);
        
if(Sys.opera) document.write('Opera: '+Sys.opera);
        
if(Sys.safari) document.write('Safari: '+Sys.safari);
    
// --></mce:script>

 这样可以使JavaScript代码更精简些。当然,可读性稍差一些,就看你是重视效率还是重视可维护性了。

      使用不同特征来判断浏览器的方法,虽然在速度上比用正则表达式分析userAgent要来的快,不过这些特征可能会随浏览器版本而变化。比如,一种浏览器本来独有的特性取得了市场上的成功,其他浏览器也就可能跟着加入该特性,从而使该浏览器的独有特征消失,导致我们的判断失败。因此,相对比较保险的做法是通过解析userAgent中的特征来判断浏览器类型。何况,反正判断版本信息也需要解析浏览器的userAgent的。

      通过分析各类浏览器的userAgent信息,不难得出分辨各类浏览器及其版本的正则表达式。而且,对浏览器类型的判断和版本的判断完全可以合为一体地进行。于是,我们可以写出下面的代码:

 <mce:script type="text/javascript"><!--
        
var Sys = {};
        
var ua = navigator.userAgent.toLowerCase();
        
var s;
        (s 
= ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] :
        (s 
= ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] :
        (s 
= ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] :
        (s 
= ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] :
        (s 
= ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0;
        
//以下进行测试
        if (Sys.ie) document.write('IE: ' + Sys.ie);
        
if (Sys.firefox) document.write('Firefox: ' + Sys.firefox);
        
if (Sys.chrome) document.write('Chrome: ' + Sys.chrome);
        
if (Sys.opera) document.write('Opera: ' + Sys.opera);
        
if (Sys.safari) document.write('Safari: ' + Sys.safari);
    
// --></mce:script>

 其中,采用了“... ? ... : ...”这样的判断表达式来精简代码。判断条件是一条赋值语句,既完成正则表达式的匹配及结果复制,又直接作为条件判断。而随后的版本信息只需从前面的匹配结果中提取即可,这是非常高效的代码。

       以上的代码都是为了打造前端框架所做的预研,并在五大浏览器上测试通过。今后,判断某种浏览器只需用if(Sys.ie)或if(Sys.firefox)等形式,而判断浏览器版本只需用if(Sys.ie == '8.0')或if(Sys.firefox == '3.0')等形式,表达起来还是非常优雅的。

遍历Request.ServerVariables 判断浏览器语言C#

浏览器型号:Request.Browser.Type
浏览器名称:Request.Browser.browser
浏览器版本:Request.Browser.Version
浏览器Cookie:Request.Browser.Cookies
你的操作系统:Request.Browser.Platform
你的IP地址:Request.UserHostAddress
<script language="JavaScript"><!--
document.write("浏览器名称: "+navigator.appName+"<br/>");
document.write("版本号: "+navigator.appVersion+"<br/>");
document.write("代码名字: "+navigator.appCodeName+"<br/>");

Request.ServerVariables("Url") ;//返回服务器地址
Request.ServerVariables("Path_Info") ;//客户端提供的路径信息
Request.ServerVariables("Appl_Physical_Path") ;//与应用程序元数据库路径相应的物理路径
Request.ServerVariables("Path_Translated") ;//通过由虚拟至物理的映射后得到的路径
Request.ServerVariables("Script_Name") ;//执行脚本的名称
Request.ServerVariables("Query_String");// 查询字符串內容
Request.ServerVariables("Http_Referer") ;//请求的字符串內容
Request.ServerVariables("Server_Port") ;//接受请求的服务器端口号
Request.ServerVariables("Remote_Addr") ;//发出请求的远程主机的IP地址
Request.ServerVariables("Remote_Host") ;//发出请求的远程主机名称
Request.ServerVariables("Local_Addr") ;//返回接受请求的服务器地址
Request.ServerVariables("Http_Host") ;//返回服务器地址
Request.ServerVariables("Server_Name");// 服务器的主机名、DNS地址或IP地址
Request.ServerVariables("Request_Method") 提出请求的方法比如GET、HEAD、POST等等
Request.ServerVariables(
"Server_Port_Secure") 如果接受请求的服务器端口为安全端口时,则为1,否则为0
Request.ServerVariables(
"Server_Protocol") 服务器使用的协议的名称和版本
Request.ServerVariables(
"Server_Software") 应答请求并运行网关的服务器软件的名称和版本
Request.ServerVariables(
"All_Http") 客户端发送的所有HTTP标头,前缀HTTP_
Request.ServerVariables(
"All_Raw") 客户端发送的所有HTTP标头,其结果和客户端发送时一样,没有前缀HTTP_
Request.ServerVariables(
"Appl_MD_Path") 应用程序的元数据库路径
Request.ServerVariables(
"Content_Length") 客户端发出內容的长度
Request.ServerVariables(
"Https") 如果请求穿过安全通道(SSL),则返回ON如果请求来自非安全通道,则返回OFF
Request.ServerVariables(
"Instance_ID") IIS实例的ID号
Request.ServerVariables(
"Instance_Meta_Path") 响应请求的IIS实例的元数据库路径
Request.ServerVariables(
"Http_Accept_Encoding") 返回內容如:gzip,deflate
Request.ServerVariables(
"Http_Accept_Language") 返回內容如:en-us
Request.ServerVariables(
"Http_Connection") 返回內容:Keep-Alive Request.ServerVariables("Http_Cookie")
Request.ServerVariables(
"Http_User_Agent") 返回內容:Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.1;SV1)
Request.ServerVariables(
"Https_Keysize") 安全套接字层连接关键字的位数,如128
Request.ServerVariables(
"Https_Secretkeysize") 服务器验证私人关键字的位数如1024
Request.ServerVariables(
"Https_Server_Issuer") 服务器证书的发行者字段
Request.ServerVariables(
"Https_Server_Subject") 服务器证书的主题字段
Request.ServerVariables(
"Auth_Password") 当使用基本验证模式时,客户在密码对话框中输入的密码
Request.ServerVariables(
"Auth_Type") 是用户访问受保护的脚本时,服务器用於检验用户的验证方法
Request.ServerVariables(
"Auth_User") 代证的用户名 Request.ServerVariables("Cert_Cookie") 唯一的客户证书ID号
Request.ServerVariables(
"Cert_Flag") 客户证书标誌,如有客户端证书,则bit0为0如果客户端证书验证无效,bit1被设置为1
Request.ServerVariables(
"Cert_Issuer") 用户证书中的发行者字段
Request.ServerVariables(
"Cert_Keysize") 安全套接字层连接关键字的位数,如128
Request.ServerVariables(
"Cert_Secretkeysize") 服务器验证私人关键字的位数如1024
Request.ServerVariables(
"Cert_Serialnumber") 客户证书的序列号字段
Request.ServerVariables(
"Cert_Server_Issuer") 服务器证书的发行者字段
Request.ServerVariables(
"Cert_Server_Subject") 服务器证书的主题字段
Request.ServerVariables(
"Cert_Subject") 客户端证书的主题字段
Request.ServerVariables(
"Content_Type") 客户发送的form內容或HTTPPUT的数据类型
动手写下 

string s =Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"].ToString(); Response.Write(s);

原文地址:https://www.cnblogs.com/lizhao/p/1990424.html