JavaScript学习笔记2:通过user-Agent获取浏览器和操作系统信息

下面是我整理的一些userAgent大家先看看格式
//Firefox 1.0.X
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6) Gecko/20050225 Firefox/1.0.1
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.8) Gecko/20050511 Firefox/1.0.4
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.10) Gecko/20050716 Firefox/1.0.6
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7
//Firefox 1.X
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Gecko/20051111 Firefox/1.5
Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.8) Gecko/20060109 Firefox/1.5 (pigfoot)
Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.2) Gecko/20060308 Firefox/1.5.0.2
Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.0.11) Gecko/20070312 Firefox/1.5.0.11
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9a1) Gecko/20060115 Firefox/1.6a1
//Firefox 2.0
Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1
Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3

//苹果的Safari 2.0.X
Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en) AppleWebKit/419 (KHTML, like Gecko) Safari/419.3
Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/417.9 (KHTML, like Gecko) Safari/417.8   

在Windows XP SP2 下, IE7 将发送其 User-Agent 头为:
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)

在Windows 2003 Server 下, IE7 将发送其 User-Agent 头为:
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)

在Windows Vista 下, IE7 将发送其 User-Agent 头为:
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)

我的机器的
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)
Maxthon
傲游 1.x 系列的 UserAgent 值是  “Maxthon”,傲游 2.0 的 UserAgent 值是 “Maxthon 2.0″。所以只需要通过程序在 User-Agent 字符串中查找是否出现 “Maxthon” 或者 “Maxthon 2.0″
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; Maxthon 2.0)
其他的
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.0.2) Gecko/20021120 Netscape/7.01
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; MyIE2)
一般来讲只需要判断浏览器的内核就可以了
像遨游1.X版本腾讯的TT浏览器都是IE6的核心
如果没有特殊需要可以不加区分
下面的例子只按浏览器核心判断是那种浏览器
//获取userAgent
var sUserAgent = window.navigator.userAgent;
//获取红色字体的部分Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)
var fAppVersion = parseFloat(window.navigator.appVersion);
//比较版本大小后面的程序会用到,如果不熟悉split和push方法请参照《JavaScript学习笔记1:Array类》
function compareVersions(sVersion1, sVersion2)
{
 var aVersion1 = sVersion1.split(".");
 var aVersion2 = sVersion2.split(".");
 
 if (aVersion1.length > aVersion2.length)
 {
  for (var i = 0; i < aVersion1.length - aVersion2.length; i++)
  {
   aVersion2.push("0");
  }
 }
 else if (aVersion1.length < aVersion2.length)
 {
  for (var i = 0; i < aVersion2.length - aVersion1.length; i++)
  {
   aVersion1.push("0");
  }
 }
 
 for (var i = 0; i < aVersion1.length; i++)
 {
  if (aVersion1[i] < aVersion2[i])
  {
   return -1;
  }
  else if (aVersion1[i] > aVersion2[i])
  {
   return 1;
  }
 }
 return 0;
}
//判断userAgent字符串里面是否有Opera字段,有则设isOpera为true,下面还会用到
var isOpera = sUserAgent.indexOf("Opera") > -1;
var isMinOpera4 = isMinOpera5 = isMinOpera6 = isMinOpera7 = isMinOpera7_5 = false;
if (isOpera)
{
 var fOperaVersion;
//Opear可以伪装成其他的浏览器,这里判断的是没有伪装的
 if (window.navigator.appName == "Opera")
 {
  fOperaVersion = fAppVersion;
 }
//这里判断的是伪装后的(某些伪装情况下在userAgent中不会出现Opera字样对这种情况我们无能为力,不过毕竟是极少
//数),关于Opera的伪装请自己查看相关资料
 else
 {
//这个有点难度了关于正则表达式的
//我们要匹配的部分(红色字体) User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; -?) Opera 7.60
  var reOperaVersion = new RegExp("Opera (\\d+\\.\\d+)");
  reOperaVersion.test(sUserAgent);
//RegExp["$1"]取出第一次匹配的结果,转换成浮点型
//Opera 7.60 这个是匹配结果,转换成浮点后只剩下数字部分7.60,刚好是我们想要的结果
  fOperaVersion = parseFloat(RegExp["$1"]);
 }
 //目前最新版本为9.20,请自行添加
 isMinOpera4 = fOperaVersion >= 4;
 isMinOpera5 = fOperaVersion >= 5;
 isMinOpera6 = fOperaVersion >= 6;
 isMinOpera7 = fOperaVersion >= 7;
 isMinOpera7_5 = fOperaVersion >= 7.5;
}
var isKHTML = sUserAgent.indexOf("KHTML") > -1 || sUserAgent.indexOf("Konqueror") > -1 || sUserAgent.indexOf("AppleWebKit") > -1;
var isMinSafari1 = isMinSafari1_2 = false;
var isMinKonq2_2 = isMinKonq3 = isMinKonq3_1 = isMinKonq3_2 = false;
if (isKHTML)
{
 var isSafari = sUserAgent.indexOf("AppleWebkit") > -1;
 var isKonq = sUserAgent.indexOf("Konqueror") > -1;
 //MacOS X的默认浏览器,基于KHTML,参考前面给出的userAgent
 if (isSafari)
 {
  var reAppleWebKit = new RegExp("AppleWebKit\\/(\\d+(?:\\.\\s*)?)");
  reAppleWebKit.test(sUserAgent);
  var fApplieWebKitVersion = parseFloat(RegExp["$1"]);
 
  isMinSafari1 = fApplieWebKitVersion >= 85;
  isMinSafari1_2 = fApplieWebKitVersion >= 124;
 }
//判断是否是Konqueror浏览器,Unix下的浏览器,基于KHTML
 else if(isKonq)
 {
  var reKong = new RegExp("Konqueror\\/(\\s+(?:\\.\\d+(?:\\.\\d)?)?)");
  reKong.test(sUserAgent);
  isMinKonq2_2 = compareVersions(RegExp["$1"], "2.2") >= 0;
  isMinKonq3 = compareVersions(RegExp["$1"], "3.0") >= 0;
  isMinKonq3_1 = compareVersions(RegExp["$1"], "3.1") >= 0;
  isMinKonq3_2 = compareVersions(RegExp["$1"], "3.2") >= 0;
 }
}
//IE
var isIE = sUserAgent.indexOf("compatible") > -1 && sUserAgent.indexOf("MSIE") > -1 && !isOpera;
var isMinIE4 = isMinIE5 = isMinIE5_5 = isMinIe6 = false;
if (isIE)
{
 var reIE = new RegExp("MSIE (\\d+\\.\\d+)");
 reIE.test(sUserAgent);
 var fIEVersion = parseFloat(RegExp["$1"]);
 
 isMinIE4 = fIEVersion >= 4;
 isMinIE5 = fIEVersion >= 5;
 isMinIE5_5 = fIEVersion >= 5.5;
 isMinIE6 = fIEVersion >= 6.0;
}
//Mozilla,火狐数此类
var isMoz = sUserAgent.indexOf("Gecko") > -1 && !isKHTML;
var isMinMoz1 = sMinMoz1_4 = isMinMoz1_5 = false;
if (isMoz)
{
//最新的版本号是4个标志位的了,这里应该把正则表达式改成"rv:(\\d+\\.\\d+(?:\\.\\d+(?:\\.\\d+)?)?)"
 var reMoz = new RegExp("rv:(\\d+\\.\\d+(?:\\.\\d+)?));
 reMoz.test(sUserAgent);
 //调用前面比较版本的方法
 isMinMoz1 = compareVersions(RegExp["$1"], "1.0") >= 0;
 isMinMoz1_4 = compareVersions(RegExp["$1"], "1.4") >= 0;
 isMinMoz1_5 = compareVersions(RegExp["$1"], "1.5") >= 0;
}
//Netscape
var isNS4 = !isIE && !isOpera && !isMoz && !isKHTML && (sUserAgent.indexOf("Mozilla") == 0) && (navigator.appName == "Netscape") && (fAppVersion >= 4.0 && fAppVersion < 5.0);
var isMinNS4 = isMinNS4_5 = isMinNS4_7 = isMinNS4_8 = false;
if (isNS4)
{
 isMinNS4 = true;
 isMinNs4_5 = fAppVersion >= 4.5;
 isMinNs4_7 = fAppVersion >= 4.7;
 isMinNs4_8 = fAppVersion >= 4.8;
}
var isWin = (window.navigator.platform == "Win32") || (window.navigator.platform == "Windows");
var isMac = (window.navigator.platform == "Mac68K") || (window.navigator.platform == "MacPPC") || (window.navigator.platform == "Macintosh");
var isUnix = (window.navigator.platform == "X11") && isWin && isMac;
var isWin95 = isWin98 = isWinNT4 = isWin2K = isWinME = isWinXP = false;
var isMac68K = isMacPPC = false;
var isSunOS = isMinSunOS4 = isMinSunOS5 = isMinSunOS5_5 = false;
if (isWin)
{
//Win2003为Windows NT 5.2
//那个vista是Windows NT 6.0
 isWin95 = sUserAgent.indexOf("Win95") >= -1 || sUserAgent.indexOf("Windows 95") >= -1;
 isWin98 = sUserAgent.indexOf("Win98") >= -1 || sUserAgent.indexOf("Windows 98") >= -1;
 isWinME = sUserAgent.indexOf("Win 9x 4.90") >= -1 || sUserAgent.indexOf("Windows ME") >= -1;
 isWin2K = sUserAgent.indexOf("Windows NT 5.0") >= -1 || sUserAgent.indexOf("Windows 2000") >= -1;
 isWinXP = sUserAgent.indexOf("Windows NT 5.1") >= -1 || sUserAgent.indexOf("Windows XP") >= -1;
 isWinNT4 = sUserAgent.indexOf("WinNT") >= -1 || sUserAgent.indexOf("WindowsNT") >= -1 || sUserAgent.indexOf("WinNT4.0") >= -1 || sUserAgent.indexOf("Windows NT 4.0") && (!isWinME && !isWin2K && !isWinXP);
}
if (isMac)
{
 isMac68K = sUserAgent.indexOf("Max_68000") > -1 || sUserAgent.indexOf("68K") > -1;
 isMacPPC = sUserAgent.indexOf("Mac_PowerPC") > -1 || sUserAgent.indexOf("PPC") > -1;
}
//Unix的系统太多了 这里只对SunOS做了判断
if (isUnix)
{
 isSunOS = sUserAgent.indexOf("SunOS") > -1;
 
 if (isSunOS)
 {
  var reSunOS = new RegExp("SunOS (\\d+\\.\\d+(?:\\.\\d+)?)");
  reSunOS.test(sUserAgent);
 
  isMinSunOS4 = compareVersions(RegExp["$1"], "4.0") >= 0;
  isMinSunOS5 = compareVersions(RegExp["$1"], "5.0") >= 0;
  isMinSunOS5_5 = compareVersions(RegExp["$1"], "5.5") >= 0;
 }
}

注意:上面的对操作系统和浏览器判断的先后顺序是不能改变的,类似于排除法
现在好多的Ajax框架里都有这个东东可以拿来直接用,
写的有点匆忙,后面很多地方和前面用的方法相同就没有复述
如果你和我一样是新手欢迎和我交流,我的邮箱是zyyn2000【at】21cn.com,最近正在学ASP.NET Ajax相关的技术,也请高手指点
如果你想看自己的userAgent在浏览器上敲这个就可以了^_^ javascript:document.write(navigator.userAgent)
这个东西我还写了一个C#版的,方便一次性识别大量的userAgent^_^
我的作文很少及格写的不好大家见笑了,欢迎批评指正。
国际惯例:如需转载请注明出处,谢谢

转载于:https://www.cnblogs.com/Ajax-cn/archive/2007/04/24/725334.html

你可能感兴趣的:(JavaScript学习笔记2:通过user-Agent获取浏览器和操作系统信息)