1、第一种方法
<script type="text/javascript">
function getExplorerInfo() {
var explorer = window.navigator.userAgent.toLowerCase() ;
//ie
if (explorer.indexOf("msie") >= 0) {
var ver=explorer.match(/msie ([\d.]+)/)[1];
return {type:"IE",version:ver};
}
//firefox
else if (explorer.indexOf("firefox") >= 0) {
var ver=explorer.match(/firefox\/([\d.]+)/)[1];
return {type:"Firefox",version:ver};
}
//Chrome
else if(explorer.indexOf("chrome") >= 0){
var ver=explorer.match(/chrome\/([\d.]+)/)[1];
return {type:"Chrome",version:ver};
}
//Opera
else if(explorer.indexOf("opera") >= 0){
var ver=explorer.match(/opera.([\d.]+)/)[1];
return {type:"Opera",version:ver};
}
//Safari
else if(explorer.indexOf("Safari") >= 0){
var ver=explorer.match(/version\/([\d.]+)/)[1];
return {type:"Safari",version:ver};
}
}
</script>
alert("type:"+getExplorerInfo().type+"\nversion:"+getExplorerInfo().version);
2、第二种方法
<script type="text/javascript">
function getBrowser() {
var Sys = {};
var ua = navigator.userAgent.toLowerCase();
var re = /(msie|firefox|chrome|opera|version).*?([\d.]+)/;
var m = ua.match(re);
Sys.browser = m[1].replace(/version/, "'safari");
Sys.ver = m[2];
return Sys.browser + Sys.ver;
}
</script>
alert("浏览器版本:"+getBrowser());
3、第三种方法
<script type="text/javascript">
//取得浏览器的userAgent字符串
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" };
}
//非IE浏览器检测
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" };
}
}
</script>
var browserMatch = uaMatch(ua);
browser = browserMatch.browser;
version = browserMatch.version;