码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-08-07 12:38:10      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   color   java   os   io   

公司最近要做一套类似于百度统计的功能,于是收集整理了其中根据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>

 

Javascript UserAgent 获取平台及浏览器信息,布布扣,bubuko.com

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

标签:android   style   blog   http   color   java   os   io   

原文地址:http://www.cnblogs.com/cxycs/p/3896568.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!