JS to determine the type of browser (whether or not to use IE, Firefox, Opera browser)
If we hope to achieve with Rails, the
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>QQ群 29032448 前端开发 </title>
<script type="text/javascript">
//----------------------------- 判断浏览器 -------------------------
function myBrowser(){
var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器
var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera ; //判断是否IE浏览器
var isFF = userAgent.indexOf("Firefox") > -1 ; //判断是否Firefox浏览器
var isSafari = userAgent.indexOf("Safari") > -1 && userAgent.indexOf("Chrome") < 1 ; //判断是否Safari浏览器
var isChrome = userAgent.indexOf("Chrome") > -1 ; //判断是否Chrome浏览器
if(isIE){
var IE5 = IE55 = IE6 = IE7 = IE8 = false;
var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
reIE.test(userAgent);
var fIEVersion = parseFloat(RegExp["$1"]);
IE55 = fIEVersion == 5.5 ;
IE6 = fIEVersion == 6.0 ;
IE7 = fIEVersion == 7.0 ;
IE8 = fIEVersion == 8.0 ;
if(IE55){ return "IE55"; }
if(IE6){ return "IE6"; }
if(IE7){ return "IE7"; }
if(IE8){ return "IE8"; }
}
if(isFF){ return "FF"; }
if(isOpera){ return "Opera"; }
if(isSafari){ return "Safari"; }
if(isChrome){ return "Chrome"; }
} //myBrowser() end
window.onload=function(){
document.getElementById("ua").innerHTML=navigator.userAgent;
if(myBrowser()=="FF"){alert("我是 Firefox");}
if(myBrowser()=="Opera"){alert("我是 Opera");}
if(myBrowser()=="Safari"){alert("我是 Safari");}
if(myBrowser()=="Chrome"){alert("我是 Chrome");}
if(myBrowser().indexOf("IE")>-1){alert("我是 IE");}
if(myBrowser()=="IE55"){alert("我是 IE5.5");}
if(myBrowser()=="IE6"){alert("我是 IE6");}
if(myBrowser()=="IE7"){alert("我是 IE7");}
if(myBrowser()=="IE8"){alert("我是 IE8");}
}
</script>
</head>
<body>
<div></div>
</body>
</html> Rails to achieve determine types of browser
def client_browser_name
user_agent =
request.env['HTTP_USER_AGENT'].downcase
if user_agent =~ /msie/i
"Internet Explorer"
elsif user_agent =~ /konqueror/i
"Konqueror"
elsif user_agent =~ /gecko/i
"Mozilla"
elsif user_agent =~ /opera/i
"Opera"
elsif user_agent =~ /applewebkit/i
"Safari"
else
"Unknown"
end
end
def getBrowser(bt)
rs=false
ua=request.env['HTTP_USER_AGENT'].downcase
isOpera = ua.index('opera') ? true : false
isSafari = (ua =~ /webkit|khtml/) ? true : false
isSafari3 = (ua.index('webkit/5') ? true : false
isGecko = (!isSafari and ua.index('gecko')) ? true : false
isGecko3 = (!isSafari and ua.index('rv:1.9')) ? true : false
isIE = (!isOpera and ua.index('msie')) ? true : false
isIE7 = (!isOpera and ua.index('msie 7')) ? true : false
case bt
when 0 #isKonqueror
if ua.index('konqueror') then rs=true end
when 1 #isOpera
rs=isOpera
when 2 #isSafari
rs=isSafari
when 3 #isSafari2
rs=isSafari && !isSafari3
when 4 #isSafari3
rs=isSafari3
when 5 #isIE
rs=isIE
when 6 #isIE6
rs=isIE && !isIE7
when 7 #isIE7
rs=isIE7
when 8 #isGecko
rs=isGecko
when 9 #isGecko2
rs=isGecko && !isGecko3
when 10 #isGecko3
rs=isGecko3
when 11 #isWindows
if ua.index('windows') or ua.index('win32') then rs=true end
when 12 #isMac
if ua.index('macintosh') or ua.index('mac os x') then rs=true
end
when 13 #isAir
if ua.index('adobeair') then rs=true end
when 14 #isLinux
if ua.index('linux') then rs=true end
when 15 #isSecure
s = request.env['SERVER_PROTOCOL'].downcase
if s.index('https') then rs=true end
end
rs
end







