判断用户使用的浏览器


function myBrowser() {
var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
if (userAgent.indexOf("Opera") > -1) {
return "Opera"
}; //判断是否Opera浏览器
if (userAgent.indexOf("Firefox") >-1) {
return "Firefox";
} //判断是否Firefox浏览器
if (userAgent.indexOf("Chrome") >-1) {
return "Chrome";
} //判断是否Google浏览器
if (userAgent.indexOf("Safari") >-1) {
return "Safari";
} //判断是否Safari浏览器
if (userAgent.indexOf("compatible") >-1 &&userAgent.indexOf("MSIE") >-1 && !isOpera) {
return "IE";
}; //判断是否IE浏览器
}

vue源码的判断:

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

var inWeex = typeof WXEnvironment !== 'undefined' && !!WXEnvironment.platform;
var weexPlatform = inWeex && WXEnvironment.platform.toLowerCase();
var UA = inBrowser && window.navigator.userAgent.toLowerCase();
var isIE = UA && /msie|trident/.test(UA);
var isIE9 = UA && UA.indexOf('msie 9.0') > 0;
var isEdge = UA && UA.indexOf('edge/') > 0;
var isAndroid = (UA && UA.indexOf('android') > 0) || (weexPlatform === 'android');
var isIOS = (UA && /iphone|ipad|ipod|ios/.test(UA)) || (weexPlatform === 'ios');
var isChrome = UA && /chrome\/\d+/.test(UA) && !isEdge;
var isPhantomJS = UA && /phantomjs/.test(UA);
var isFF = UA && UA.match(/firefox\/(\d+)/);

 

posted @ 2019-09-26 14:37  你风致  阅读(621)  评论(0编辑  收藏  举报