<div id="example"></div>
<script>
txt = "<p>Browser CodeName: " + navigator.appCodeName + "</p>"
txt+= "<p>Browser Name: " + navigator.appName + "</p>"
txt+= "<p>Browser Version: " + navigator.appVersion + "</p>"
txt+= "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>"
txt+= "<p>Platform: " + navigator.platform + "</p>"
txt+= "<p>User-agent header: " + navigator.userAgent + "</p>"
txt+= "<p>User-agent language: " + navigator.systemLanguage + "</p>"
document.getElementById("example").innerHTML=txt
</script>
可以查看浏览器版本信息
来自 navigator 对象的信息具有误导性,不应该被用于检测浏览器版本,这是因为:
1.navigator 数据可被浏览器使用者更改
2.浏览器无法报告晚于浏览器发布的新操作系统
参考 w3school
var browser = getBrowserInfo() //浏览器信息//alert(browser)//IE 11.0
//IE11以下: MSIE 10.0、MSIE9.0等
//chrome:chrome/41.0.2272.89 [返回的是个数组]
//firefox: firefox/42.0 [返回的是个数组]
var verinfo = (browser+"").replace(/[^0-9.]/ig,"")//浏览器版本
//alert(verinfo)//11.0
//IE浏览器: 11.0/10.0/9.0
//chrome浏览器:41.0.2272.89
//Firefox浏览器: 42.0
function getBrowserInfo()
{
var agent = navigator.userAgent.toLowerCase()
//console.log(agent)
//agent chrome : mozilla/5.0 (windows nt 6.1wow64) applewebkit/537.36 (khtml, like gecko) chrome/41.0.2272.89 safari/537.36
//agent firefox : mozilla/5.0 (windows nt 6.1wow64rv:42.0) gecko/20100101 firefox/42.0
//agent IE11: mozilla/5.0 (windows nt 6.1wow64trident/7.0slcc2.net clr 2.0.50727.net clr 3.5.30729
//接上.net clr 3.0.30729media center pc 6.0infopath.2.net4.0c.net4.0erv:11.0) like gecko
(可以看出IE11中不包括MSIE字段)
//agent IE10: mozilla/5.0(compatiblemsie 10.0windows nt 6.1wow64trident/6.0)
var regStr_ie = /msie [\d.]+/gi
var regStr_ff = /firefox\/[\d.]+/gi
var regStr_chrome = /chrome\/[\d.]+/gi
var regStr_saf = /safari\/[\d.]+/gi
//IE11以下
if(agent.indexOf("msie") >0)
{
return agent.match(regStr_ie)
}
//IE11版本中不包括MSIE字段
if(agent.indexOf("trident") >0&&agent.indexOf("rv") >0){
return "IE " + agent.match(/rv:(\d+\.\d+)/) [1]
}
//firefox
if(agent.indexOf("firefox") >0)
{
return agent.match(regStr_ff)
}
//Chrome
if(agent.indexOf("chrome") >0)
{
return agent.match(regStr_chrome)
}
//Safari
if(agent.indexOf("safari") >0 &&agent.indexOf("chrome") <0)
{
return agent.match(regStr_saf)
}
}