Skip to content

Instantly share code, notes, and snippets.

@zhanghqgit
Created May 9, 2017 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhanghqgit/315fed5452d9994b84a3096df7ca302c to your computer and use it in GitHub Desktop.
Save zhanghqgit/315fed5452d9994b84a3096df7ca302c to your computer and use it in GitHub Desktop.
各浏览器的UserAgent大全

webkit

userAgent.match(/WebKit\/([\d.]+)/)

android

  • 安卓通用版本: userAgent.match(/Silk-Accelerated/)
  • 安卓4.0版本,代号为ISC(冰激凌): userAgent.match(/(Android)\s4/)
  • 安卓特殊的版本kindle: userAgent.match(/(Android)\s+([\d.]+)/)

IOS

  • ipad: userAgent.match(/(iPad).*OS\s([\d_]+)/)
  • iphone: userAgent.match(/(iPhone\sOS)\s([\d_]+)/)
  • ios7: (ipad 或者 iphone) && userAgent.match(/7_/)||userAgent.match(/8_/)

WebOS

userAgent.match(/(webOS|hpwOS)[\s\/]([\d.]+)/)

touchpad惠普搭载webOS的产品: userAgent.match(/TouchPad/)

blackBerry黑莓
  • playbook黑莓产品: userAgent.match(/PlayBook/)
  • blackberry10: userAgent.match(/BB10/)
  • blackberry: userAgent.match(/BlackBerry/)

chrome

userAgent.match(/Chrome/)

opera

userAgent.match(/Opera/)

firefox

  • fennec: Firefox浏览器针对移动电话和非个人计算机设备推出的一个浏览器版本: userAgent.match(/fennec/i)
  • 其它通用的firefox浏览器: userAgent.match(/Firefox/)

ie

  • ie10之前: userAgent.match(/MSIE 10.0/i)
  • ie11之后: userAgent.match(/Trident\/7/i)
  • ieTouch触摸版: ie && userAgent.toLowerCase().match(/touch/i)
  • Tizen: userAgent.match(/Tizen/i)

判断是否支持触摸

((window.DocumentTouch && document instanceof window.DocumentTouch) || "ontouchstart" in window)

整合

os.webkit = userAgent.match(/WebKit\/([\d.]+)/) ? true : false;

os.android = userAgent.match(/(Android)\s+([\d.]+)/) || userAgent.match(/Silk-Accelerated/) ? true : false;
os.androidICS = $.os.android && userAgent.match(/(Android)\s4/) ? true : false;

os.ipad = userAgent.match(/(iPad).*OS\s([\d_]+)/) ? true : false;
os.iphone = !$.os.ipad && userAgent.match(/(iPhone\sOS)\s([\d_]+)/) ? true : false;
os.ios7 = ($.os.ipad||$.os.iphone)&&(userAgent.match(/7_/)||userAgent.match(/8_/)) ? true : false;
os.ios = $.os.ipad || $.os.iphone;

os.webos = userAgent.match(/(webOS|hpwOS)[\s\/]([\d.]+)/) ? true : false;
os.touchpad = $.os.webos && userAgent.match(/TouchPad/) ? true : false;

os.playbook = userAgent.match(/PlayBook/) ? true : false;
os.blackberry10 = userAgent.match(/BB10/) ? true : false;
os.blackberry = $.os.playbook || $.os.blackberry10|| userAgent.match(/BlackBerry/) ? true : false;

os.chrome = userAgent.match(/Chrome/) ? true : false;
os.opera = userAgent.match(/Opera/) ? true : false;
os.fennec = userAgent.match(/fennec/i) ? true : userAgent.match(/Firefox/) ? true : false;

os.ie = userAgent.match(/MSIE 10.0/i)||userAgent.match(/Trident\/7/i) ? true : false;
os.ieTouch = $.os.ie && userAgent.toLowerCase().match(/touch/i) ? true : false;

os.tizen = userAgent.match(/Tizen/i)?true:false;

os.kindle=userAgent.match(/Silk-Accelerated/)?true:false;

os.supportsTouch = ((window.DocumentTouch && document instanceof window.DocumentTouch) || "ontouchstart" in window);
// 他人案例
function parseUA() {
            var u = navigator.userAgent;
            var u2 = navigator.userAgent.toLowerCase();
            return { //移动终端浏览器版本信息
                trident: u.indexOf('Trident') > -1, //IE内核
                presto: u.indexOf('Presto') > -1, //opera内核
                webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
                gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
                mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
                ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
                android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或uc浏览器
                iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器
                iPad: u.indexOf('iPad') > -1, //是否iPad
                webApp: u.indexOf('Safari') == -1, //是否web应该程序,没有头部与底部
                iosv: u.substr(u.indexOf('iPhone OS') + 9, 3),
                weixin: u2.match(/MicroMessenger/i) == "micromessenger",
                ali: u.indexOf('AliApp') > -1,
            };
        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment