2012-06-19 57 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.