ua = navigator.userAgent;
if (window.ActiveXObject)
Sys.ie = ua.match(/MSIE ([\d.]+)/)[1]
else if (window.google && window.chrome){
Sys.chrome = ua.match(/Chrome\/([\d.]+)/)[1]}
else if (window.Components)
Sys.firefox = ua.match(/Firefox\/([\d.]+)/)[1]
else if (window.opera)
Sys.opera = ua.match(/Opera.([\d.]+)/)[1]
else if (!navigator.taintEnabled)
Sys.safari = ua.match(/Version\/([\d.]+)/)[1];
//以下进行测试
if(Sys.ie) document.write('IE: '+Sys.ie);
if(Sys.firefox) document.write('Firefox: '+Sys.firefox);
if(Sys.chrome) document.write('Chrome: '+Sys.chrome);
if(Sys.opera) document.write('Opera: '+Sys.opera);
if(Sys.safari) document.write('Safari: '+Sys.safari);
第二种方法:
var ua=navigator.userAgent,
b = {
ie : !!window.ActiveXObject,
//!+'\v1'
//!!window.VBArray
ie6 : !"1"[0],
ie67 : /MSIE [67]/.test(ua),
ie9 : !!document.documentMode && document.documentMode===9,
chrome : !!(window.google && window.chrome),
firefox : !!window.Components,
safari : !!navigator.vendor && !top.chrome,//(!navigator.taintEnabled && !window.chrome), 奇怪的IE6/7报错,
opera : !!window.opera,
version : (ua.match(/\s+(?:MSIE|Chrome|Firefox|Version)[ \/]([\d.]+)/) || [0,0])[1]
};
return b
}()
//显示结果
document.write("<br/><br/>"+navigator.userAgent+"<br/>")
var c=browser;
for(var n in c){
document.write(n+" : "+c[n]+"<br/>")
}
第三种方法:ua检测
var ua=navigator.userAgent,
check=function(r){return r.test(ua)},
b = {
ie : check(/MSIE/),
ie6 : check(/MSIE 6/),
ie67 : check(/MSIE [67]/),
ie9 : check(/MSIE 9/),
chrome : check(/Chrome/),
firefox : check(/Firefox/),
safari : check(/Safari/) && !top.chrome,
opera : check(/Opera/),
version : (ua.match(/\s+(?:MSIE|Chrome|Firefox|Version)[ \/]([\d.]+)/) || [0,0])[1]
};
return b
}()
js chrome浏览器判断代码
早期我给出的判定是基于UA,在UA中它有一个独一无二的Chrome字符串。
复制代码 代码如下:
var isChrome = window.navigator.userAgent.indexOf("Chrome") !== -1
今天发现chrome还是有一些独一无二的东西,基于它们,我们就可以搞出基于特征的判定了。
复制代码 代码如下:
var isChrome = window.google && window.chrome
这两个东西,前者拥有其本地数据库gears,后者不知什么东东,有两个属性csi与loadTimes。
(责任编辑:ken)