判断浏览器 终端

3,615 阅读1分钟

通过判断window是否是undefined,来判断运行环境是否是浏览器,在浏览器中,window不是undefined

  • var inBrowser = typeof window !== 'undefined';

判断代码是否运行在weex环境中,weex是阿里推出的一个库,可以用前端的js、html、css写移动端的项目

  • var inWeex = typeof WXEnvironment !== 'undefined' && !!WXEnvironment.platform;

判断是weex下的移动端的哪种环境

  • var weexPlatform = inWeex && WXEnvironment.platform.toLowerCase();

BOM中的navigation

  • var UA = inBrowser && window.navigator.userAgent.toLowerCase();

判断是不是ie

  • var isIE = UA && /msie|trident/.test(UA);

判断是不是ie9

  • var isIE9 = UA && UA.indexOf('msie 9.0') > 0;

判断是不是edge

  • var isEdge = UA && UA.indexOf('edge/') > 0;

判断是不是安卓

  • var isAndroid = (UA && UA.indexOf('android') > 0) || (weexPlatform === 'android');

判断是不是 ios

  • var isIOS = (UA && /iphone|ipad|ipod|ios/.test(UA)) || (weexPlatform === 'ios');

判断是不是chrome

  • var isChrome = UA && /chrome/\d+/.test(UA) && !isEdge;

判断是不是 isPhantomJS。

  • var isPhantomJS = UA && /phantomjs/.test(UA);

判断是不是 火狐

  • var isFF = UA && UA.match(/firefox/(\d+)/);
-- 代码来自vue源码