A:
var Sys = {},
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);
B:
var browser=function(){//尽量使用特性去检测
var ua=navigator.userAgent,
b = {
ie : !!window.ActiveXObject,
ie67 : /MSIE [67]/.test(ua),
chrome : !!(window.google && window.chrome),
firefox : !!window.Components,// /Firefox/.test(ua),
safari : /Safari/.test(ua),//(!navigator.taintEnabled && !window.chrome),
opera : !!window.opera,
version : (ua.match(/\s+(?:MSIE|Chrome|Firefox|Version)[ \/](\d+\.\d*)/) || [0,0])[1]
};
return b
}()
//显示结果
document.write("<br/>"+navigator.userAgent+"<br/>")
var c=browser;
for(var n in c){
document.write(n+":"+c[n]+"<br/>")
}