//写的一个判断浏览器类型和版本的Demo ^_^
/** +---------------------------------------------------------- * 获取浏览器类型和版本 by cc +---------------------------------------------------------- * OUT:(ret(Array),详细请看代码注释) +---------------------------------------------------------- */ function getBrowserVer() { var ret = new Array(); //返回的数组 ret['kernelType'] = "null"; //内核类型:MSIE,Firefox,Opera,Chrome ret['kernelMajorVer'] = 0; //内核的主版本 ret['kernelMinorVer'] = 0; //内核的次版本 ret['browserType'] = "null";//浏览器类型:TheWorld,360SE,Maxthon(2.0,3.0) ret['browserMajorVer'] = 0; //浏览器的主版本 ret['browserMinorVer'] = 0; //浏览器的次版本 var agent = window.userAgent; var str = ""; if(/MSIE \d*\.\d*/.test(agent)) //IE { //判断内核类型 str = /MSIE \d*\.\d*/.exec(agent); ret['kernelType'] = "MSIE"; //判断浏览器类型 if(/TheWorld/.test(agent)) ret['browserType'] = "TheWorld"; //世界之窗浏览器 if(/360SE/.test(agent)) ret['browserType'] = "360SE"; //360安全浏览器 if(/Maxthon/.test(agent)) { ret['browserType'] = "Maxthon"; //遨游浏览器 if(/Maxthon 2.0/.test(agent)) //2.0版本,3.0版本的兼容模式没有写版本号 { ret['browserMajorVer'] = 2; ret['browserMinorVer'] = 0; } else { ret['browserMajorVer'] = 3; ret['browserMinorVer'] = 0; } } } if(/Firefox/.test(agent)) //firefox { str = /Firefox\/\d*\.\d*/.exec(agent); ret['kernelType'] = "Firefox"; } if(/Opera\/\d*\.\d*/.test(agent)) //opera { str = /Opera\/\d*.\d*/.exec(agent); ret['kernelType'] = "Opera"; } if(/Chrome\/\d*.\d*/.test(agent)) //Chrome { str = /Chrome\/\d*\.\d*/.exec(agent); ret['kernelType'] = "Chrome"; } if(ret['kernelType'] != "null") { ret['kernelMajorVer'] = /\d*/.exec(/\d*\./.exec(str)); ret['kernelMinorVer'] = /\d*$/.exec(/\.\d*/.exec(str)); } return ret; }
|
原文地址:http://hi.baidu.com/_ccsec/blog/item/3abaa204508e689ad53f7c49.html