使用js代码获取浏览器版本信息如下:
例如:ie9.0 firefox34.0 chrome37.0等等
/** * Get browser name agent version * return browser name version * */ function getBrowserNameAndVersion(){ var agent = navigator.userAgent.toLowerCase(); var regStr_ie = /msie [\d.]+/gi ; var regStr_ff = /firefox\/[\d.]+/gi var regStr_chrome = /chrome\/[\d.]+/gi ; var regStr_saf = /safari\/[\d.]+/gi ; var browserNV = ""; //IE if(agent.indexOf("msie") > 0) { browserNV = agent.match(regStr_ie) ; } //firefox if(agent.indexOf("firefox") > 0) { browserNV = agent.match(regStr_ff) ; } //Chrome if(agent.indexOf("chrome") > 0) { browserNV = agent.match(regStr_chrome) ; } //Safari if(agent.indexOf("safari") > 0 && agent.indexOf("chrome") < 0) { browserNV = agent.match(regStr_saf) ; } browserNV = browserNV.toString(); //other if(""==browserNV){ browserNV = "Is not a standard browser"; } //Here does not display "/" if(browserNV.indexOf('firefox')!= -1 || browserNV.indexOf('chrome')!= -1){ browserNV = browserNV.replace("/",""); } //Here does not display space if(browserNV.indexOf('msie')!= -1){ //msie replace IE & trim space browserNV = browserNV.replace("msie","ie").replace(/\s/g,""); } //return eg:ie9.0 firefox34.0 chrome37.0 return browserNV; }
获取浏览器其他信息(可以从中截取字符串,判断其他信息):
var browver = getBrowserNameAndVersion(); var bname = bsnav("name"); var bagent = bsnav("agent"); var bversion = bsnav("version"); alert("loading……" + browver); alert("version: "+bversion); alert("agernt: "+bagent); alert("name: "+bname);
/** * Get browser name agent version * parm getString * return getInfo * */ function bsnav(string){ switch (string) { case "version": var browser_version = navigator.appVersion; return browser_version; // break; case "agent": var browser_agent = navigator.userAgent; return browser_agent; // break; case "name": var browser_name = navigator.appName; return browser_name; // break; default: var browser_no = "Input parameter error"; return browser_no; // break; } }
效果图: