How does js determine the browser types of different systems

  • 2020-03-26 21:42:56
  • OfStack

 
function Env(){ 
var ua=navigator.userAgent.toLowerCase(); 
function check(r){ 
return r.test(ua); 
} 
return { 
//Determine the environment, operating system, browser, HTTPS connection, and so on
DOC : document, 
isStrict : DOC.compatMode == "CSS1Compat" , 
isOpera : check(/opera/) , 
isChrome : check(/bchromeb/) , 
isWebKit : check(/webkit/) , 
isSafari : !check(/bchromeb/)&& check(/safari/) , 
isSafari2 : !check(/bchromeb/)&& check(/safari/)&& check(/applewebkit/4/), // unique to Safari 2 
isSafari3 : !check(/bchromeb/)&& check(/safari/)&& check(/version/3/), 
isSafari4 : !check(/bchromeb/)&& check(/safari/)&& check(/version/4/), 
isIE : !check(/opera/) && check(/msie/) , 
isIE7 : !check(/opera/) && check(/msie/)&& check(/msie 7/) , 
isIE8 : !check(/opera/) && check(/msie/)&& check(/msie 8/) , 
isIE6 : !check(/opera/) && check(/msie/)&&!check(/msie 7/)&& !check(/msie 8/), 
isGecko : !check(/webkit/)&& check(/gecko/), 
isGecko2 : check(/webkit/)&& check(/rv:1.8/), 
isGecko3 : check(/webkit/)&& check(/rv:1.9/), 
isBorderBox : !check(/opera/) && check(/msie/)&& DOC.compatMode != "CSS1Compat", 
isWindows : check(/windows|win32/), 
isMac : check(/macintosh|mac os x/), 
isAir : check(/adobeair/), 
isLinux : check(/linux/), 
isSecure : /^https/i.test(window.location.protocol), 
 
isEmpty : function(v, allowBlank){ 
return v === null || v === undefined || ((this.isArray(v) && !v.length)) || (!allowBlank ? v === '' : false); 
}, 

 
isArray : function(v){ 
return toString.apply(v) === '[object Array]'; 
}, 

 
isDate : function(v){ 
return toString.apply(v) === '[object Date]'; 
}, 

 
isObject : function(v){ 
return !!v && Object.prototype.toString.call(v) === '[object Object]'; 
}, 

 
isFunction : function(v){ 
return toString.apply(v) === '[object Function]'; 
}, 

 
isNumber : function(v){ 
return typeof v === 'number' && isFinite(v); 
}, 

 
isString : function(v){ 
return typeof v === 'string'; 
}, 

 
isBoolean : function(v){ 
return typeof v === 'boolean'; 
}, 

 
isElement : function(v) { 
return !!v && v.tagName; 
}, 

 
isDefined : function(v){ 
return typeof v !== 'undefined'; 
} 
} 

And then var env = env(); To fetch the desired type using env.s.

Related articles: