JS获取当前浏览器名及版本号
<!DOCTYPE>
<html>
<head>
<title></title>
</head>
<script type="text/javascript">
// 获取当前浏览器名 及 版本号
function appInfo(){
var browser = {appname: 'unknown', version: 0},
userAgent = window.navigator.userAgent.toLowerCase(); // 使用navigator.userAgent来判断浏览器类型
//msie,firefox,opera,chrome,netscape
if ( /(msie|firefox|opera|chrome|netscape)\D+(\d[\d.]*)/.test( userAgent ) ){
browser.appname = RegExp.$1;
browser.version = RegExp.$2;
} else if ( /version\D+(\d[\d.]*).*safari/.test( userAgent ) ){ // safari
browser.appname = 'safari';
browser.version = RegExp.$2;
}
return browser;
}
var testBrowser = appInfo();
var browerName=testBrowser.appname;
var browerVersion=testBrowser.version;
var exite=browerName.indexOf("msie"); // 若包含msie则返回0 不包含返回-1
alert(browerName);
alert(browerVersion);
</script>
<body>
</body>
</html>
第二种兼容性比较好
window.onload = function ()
{
var userAgent = navigator.userAgent,
rMsie = /(msie\s|trident.*rv:)([\w.]+)/,
rFirefox = /(firefox)\/([\w.]+)/,
rOpera = /(opera).+version\/([\w.]+)/,
rChrome = /(chrome)\/([\w.]+)/,
rSafari = /version\/([\w.]+).*(safari)/;
var browser;
var version;
var ua = userAgent.toLowerCase();
function uaMatch(ua){
var match = rMsie.exec(ua);
if(match != null){
return { browser : "IE", version : match[2] || "0" };
}
var match = rFirefox.exec(ua);
if (match != null) {
return { browser : match[1] || "", version : match[2] || "0" };
}
var match = rOpera.exec(ua);
if (match != null) {
return { browser : match[1] || "", version : match[2] || "0" };
}
var match = rChrome.exec(ua);
if (match != null) {
return { browser : match[1] || "", version : match[2] || "0" };
}
var match = rSafari.exec(ua);
if (match != null) {
return { browser : match[2] || "", version : match[1] || "0" };
}
if (match != null) {
return { browser : "", version : "0" };
}
}
var browserMatch = uaMatch(userAgent.toLowerCase());
if (browserMatch.browser){
browser = browserMatch.browser;
version = browserMatch.version;
}
var vcode = parseInt(version); //强制转成整形
if(vcode<9){
window.location.href="tanchuang.html";
}else{
window.history.back(-1);
}
};