不考虑对 IE9 以下浏览器的判断
function myBrowser(){
var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
var browser='unknown';
if (userAgent.indexOf("IE")!=-1) {
browser="IE";
}else if(userAgent.indexOf('Firefox')!=-1){
browser="Firefox";
}else if(userAgent.indexOf('OPR')!=-1){
browser="Opera";
}else if(userAgent.indexOf('Chrome')!=-1){
browser="Chrome";
}else if(userAgent.indexOf('Safari')!=-1){
browser="Safari";
}else if(userAgent.indexOf('Trident')!=-1){
browser='IE 11';
}
return browser;
}
单独对 IE9 以下浏览器进行判断
function ieBrowser() {
var browser = navigator.appName;
var version = navigator.appVersion.split(";")[1].replace(/[ ]/g,"");
if(browser == "Microsoft Internet Explorer" && version == "MSIE6.0") {
return "IE 6";
} else if(browser == "Microsoft Internet Explorer" && version == "MSIE7.0") {
return "IE 7";
} else if(browser == "Microsoft Internet Explorer" && version == "MSIE8.0") {
return "IE 8";
}
}