2012-06-19 131 views
-1

可能重复:
Best way to find Browser type & version?如何识别浏览器?

在HTML/JavaScript的,我怎么能识别用户正在使用的浏览器,并通过选择的代码,我的意思是:如果浏览器crhome这么做...其他... 谢谢

+0

取决于你想做什么,你可以使用功能检测,而不是 – Esailija

+2

拜托你知道如何寻找的东西在谷歌不您? http://www.google.nl/search?hl=zh-CN&q=How+to+identify+the+browser – albertjan

+0

检查Javascript中的导航器对象 –

回答

5

你可以使用navigator并获得所有的细节。只需进行谷歌搜索!

<div id="example"></div> 

<script type="text/javascript"> 

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>"; 

document.getElementById("example").innerHTML=txt; 

</script> 

希望这有助于! :)

+1

其信息是不正确的,它显示mozzila&netscape o_o –

+0

嘿,它显示了一切。浏览器的细节。你必须在使用之前操作它。 –

4

由于浏览器是移动目标,因此不建议检测浏览器。例如,今天的Chrome不支持功能X,但几个月后它开始支持它。如果你简单地把

// Here I detect Chrome browser without a third-party library 
if (navigator.userAgent.toLowerCase().indexOf('chrome') === -1) { 
    doFeatureX() 
else { 
    console.log('Sorry, no Feature X for you.') 
} 

那么你锁定Chrome用户进行永远。如果你想

if (Modernizr.geolocation) { 
    // do stuff with user's coordinates 
} 

但当然你可以检测浏览器:你应该做的,而不是feature detection有不少libraries来帮你。例如,你想鼓励人们升级他们的浏览器:

// Here I'm using jQuery 
if ($.browser.msie && parseInt($.browser.version, 10) < 9) { 
    // show link to http://microsoft.com/ie 
} else if ($.browser.mozilla && parseInt($.browser.version, 10) < 11) { 
    // show link to http://mozilla.com/firefox 
} // etc.