首页 > 代码库 > Javascript UserAgent 获取平台及浏览器信息

Javascript UserAgent 获取平台及浏览器信息

公司最近要做一套类似于百度统计的功能,于是收集整理了其中根据UserAgent获取客户端平台及浏览器信息的资料,不仅仅试用于Javascript,其他语言也可以参考相应正则进行改进。当然,肯定有不足的地方,希望大家多多帮忙测试补充完善。

在线测试地址:http://blog.cxycs.com/demos/client/browser.html

Javascript代码:

<script type="text/javascript">document.getElementById("useragent").innerHTML = navigator.userAgent.toLocaleUpperCase();window.document.getElementById("platform").innerHTML = getPlatformName();window.document.getElementById("browser").innerHTML = getBrowserName();function getPlatformName(){    var xp = /WINDOWS NT 5.[\d]+/;    var win7 = /WINDOWS NT 6.[\d]+/;    var win8 = /WINDOWS NT 7.[\d]+/;    var winphone = /WINDOWS PHONE/;    var android = /ANDROID [\d\.]+/;    var iphone = /IPHONE [\d\_]+/;    var ipad = /IPAD/;    var userAgent = navigator.userAgent.toLocaleUpperCase();    if (xp.test(userAgent)) return ‘Windows XP‘;    if (win7.test(userAgent)) return ‘Windows 7‘;    if (win8.test(userAgent)) return ‘Windows 8‘;    if (winphone.test(userAgent)) return ‘Windows Phone‘;    if (android.test(userAgent)) return android.exec(userAgent);    if (iphone.test(userAgent)) return ‘IPhone‘;    if (ipad.test(userAgent)) return ‘IPad‘;    return ‘Other Platform‘;}function getBrowserName(){    var userAgent = navigator.userAgent.toLocaleUpperCase();    var msie = /MSIE [\d\.]+/;    var firefox = /FIREFOX\/[\d\.]+/;    var chrome = /CHROME\/[\d\.]+/;    var safari = /SAFARI\/[\d\.]+/;    var opero = /OPR\/[\d\.]+/;    var se = /SE \d/;    var mi = /XIAOMI\/MIUIBROWSER/;    var uc = /UCBROWSER/;    var android = /ANDROID [\d\.]+/;    if (msie.test(userAgent) && se.test(userAgent)) return ‘搜狗浏览器兼容模式‘;    if (msie.test(userAgent)) return msie.exec(userAgent);    if (se.test(userAgent)) return ‘搜狗浏览器高速模式‘;    if (uc.test(userAgent)) return ‘UC浏览器‘;    //if (mi.test(userAgent)) return ‘小米手机内置浏览器‘;    if (android.test(userAgent)) return ‘安卓手机浏览器‘;    if (opero.test(userAgent)) return ‘opera浏览器‘;    if (chrome.test(userAgent)) return chrome.exec(userAgent);    if (safari.test(userAgent)) return safari.exec(userAgent);    return ‘其他‘;}</script>