如何使用JavaScript检测我的浏览器版本和操作系统?

我曾尝试使用下面的代码,但它只在Chrome和Mozilla中显示结果不能在IE6中工作。

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

输出:

 Browser CodeName: Mozilla Browser Name: Netscape Browser Version: 5.0 (Windows) Cookies Enabled: true Platform: Win32 User-agent header: Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20100101 Firefox/12.0 

我只需要获得“Firefox / 12.0”版本。

检测浏览器的详细信息:

 var nVer = navigator.appVersion; var nAgt = navigator.userAgent; var browserName = navigator.appName; var fullVersion = ''+parseFloat(navigator.appVersion); var majorVersion = parseInt(navigator.appVersion,10); var nameOffset,verOffset,ix; // In Opera, the true version is after "Opera" or after "Version" if ((verOffset=nAgt.indexOf("Opera"))!=-1) { browserName = "Opera"; fullVersion = nAgt.substring(verOffset+6); if ((verOffset=nAgt.indexOf("Version"))!=-1) fullVersion = nAgt.substring(verOffset+8); } // In MSIE, the true version is after "MSIE" in userAgent else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) { browserName = "Microsoft Internet Explorer"; fullVersion = nAgt.substring(verOffset+5); } // In Chrome, the true version is after "Chrome" else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) { browserName = "Chrome"; fullVersion = nAgt.substring(verOffset+7); } // In Safari, the true version is after "Safari" or after "Version" else if ((verOffset=nAgt.indexOf("Safari"))!=-1) { browserName = "Safari"; fullVersion = nAgt.substring(verOffset+7); if ((verOffset=nAgt.indexOf("Version"))!=-1) fullVersion = nAgt.substring(verOffset+8); } // In Firefox, the true version is after "Firefox" else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) { browserName = "Firefox"; fullVersion = nAgt.substring(verOffset+8); } // In most other browsers, "name/version" is at the end of userAgent else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) < (verOffset=nAgt.lastIndexOf('/')) ) { browserName = nAgt.substring(nameOffset,verOffset); fullVersion = nAgt.substring(verOffset+1); if (browserName.toLowerCase()==browserName.toUpperCase()) { browserName = navigator.appName; } } // trim the fullVersion string at semicolon/space if present if ((ix=fullVersion.indexOf(";"))!=-1) fullVersion=fullVersion.substring(0,ix); if ((ix=fullVersion.indexOf(" "))!=-1) fullVersion=fullVersion.substring(0,ix); majorVersion = parseInt(''+fullVersion,10); if (isNaN(majorVersion)) { fullVersion = ''+parseFloat(navigator.appVersion); majorVersion = parseInt(navigator.appVersion,10); } document.write('' +'Browser name = '+browserName+'<br>' +'Full version = '+fullVersion+'<br>' +'Major version = '+majorVersion+'<br>' +'navigator.appName = '+navigator.appName+'<br>' +'navigator.userAgent = '+navigator.userAgent+'<br>' ) 

源JavaScript:浏览器名称 。
请参阅JSFiddle以检测浏览器详细信息 。

检测操作系统:

 // This script sets OSName variable as follows: // "Windows" for all versions of Windows // "MacOS" for all versions of Macintosh OS // "Linux" for all versions of Linux // "UNIX" for all other UNIX flavors // "Unknown OS" indicates failure to detect the OS var OSName="Unknown OS"; if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows"; if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS"; if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX"; if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux"; document.write('Your OS: '+OSName); 

源码JavaScript:操作系统检测 。
查看JSFiddle来检测操作系统细节 。

  var nVer = navigator.appVersion; var nAgt = navigator.userAgent; var browserName = navigator.appName; var fullVersion = ''+parseFloat(navigator.appVersion); var majorVersion = parseInt(navigator.appVersion,10); var nameOffset,verOffset,ix; // In Opera, the true version is after "Opera" or after "Version" if ((verOffset=nAgt.indexOf("Opera"))!=-1) { browserName = "Opera"; fullVersion = nAgt.substring(verOffset+6); if ((verOffset=nAgt.indexOf("Version"))!=-1) fullVersion = nAgt.substring(verOffset+8); } // In MSIE, the true version is after "MSIE" in userAgent else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) { browserName = "Microsoft Internet Explorer"; fullVersion = nAgt.substring(verOffset+5); } // In Chrome, the true version is after "Chrome" else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) { browserName = "Chrome"; fullVersion = nAgt.substring(verOffset+7); } // In Safari, the true version is after "Safari" or after "Version" else if ((verOffset=nAgt.indexOf("Safari"))!=-1) { browserName = "Safari"; fullVersion = nAgt.substring(verOffset+7); if ((verOffset=nAgt.indexOf("Version"))!=-1) fullVersion = nAgt.substring(verOffset+8); } // In Firefox, the true version is after "Firefox" else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) { browserName = "Firefox"; fullVersion = nAgt.substring(verOffset+8); } // In most other browsers, "name/version" is at the end of userAgent else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) < (verOffset=nAgt.lastIndexOf('/')) ) { browserName = nAgt.substring(nameOffset,verOffset); fullVersion = nAgt.substring(verOffset+1); if (browserName.toLowerCase()==browserName.toUpperCase()) { browserName = navigator.appName; } } // trim the fullVersion string at semicolon/space if present if ((ix=fullVersion.indexOf(";"))!=-1) fullVersion=fullVersion.substring(0,ix); if ((ix=fullVersion.indexOf(" "))!=-1) fullVersion=fullVersion.substring(0,ix); majorVersion = parseInt(''+fullVersion,10); if (isNaN(majorVersion)) { fullVersion = ''+parseFloat(navigator.appVersion); majorVersion = parseInt(navigator.appVersion,10); } document.write('' +'Browser name = '+browserName+'<br>' +'Full version = '+fullVersion+'<br>' +'Major version = '+majorVersion+'<br>' +'navigator.appName = '+navigator.appName+'<br>' +'navigator.userAgent = '+navigator.userAgent+'<br>' ) // This script sets OSName variable as follows: // "Windows" for all versions of Windows // "MacOS" for all versions of Macintosh OS // "Linux" for all versions of Linux // "UNIX" for all other UNIX flavors // "Unknown OS" indicates failure to detect the OS var OSName="Unknown OS"; if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows"; if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS"; if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX"; if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux"; document.write('Your OS: '+OSName); 

我很伤心地说:我们这次不走运。

我想向你推荐WhichBrowser的作者: 每个人都在说谎

基本上,没有浏览器是诚实的。 无论你使用的是Chrome还是IE,他们都会告诉你他们是支持Gecko和Safari的“Mozilla网景”。 尝试一下自己的任何在这个线程中飞来飞去的小提琴:

hims056的小提琴

哈里哈兰的小提琴

或任何其他…尝试与Chrome浏览器(这可能仍然成功),然后尝试与最新版本的IE浏览器,你会哭了。 当然,也有启发式的做法,但是要把握好所有的边缘案例是非常繁琐的,一年之内很可能不再工作。

拿你的代码,例如:

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

Chrome说:

浏览器CodeName:Mozilla

浏览器名称:Netscape

浏览器版本:5.0(Windows NT 6.1; WOW64)AppleWebKit / 537.36(KHTML,如Gecko)Chrome / 40.0.2214.115 Safari / 537.36

Cookies已启用:true

平台:Win32

用户代理头:Mozilla / 5.0(Windows NT 6.1; WOW64)AppleWebKit / 537.36(KHTML,如Gecko)Chrome / 40.0.2214.115 Safari / 537.36

IE说:

浏览器CodeName:Mozilla

浏览器名称:Netscape

浏览器版本:5.0(Windows NT 6.1; WOW64; Trident / 7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET 4.0C; .NET4 .E; InfoPath.3; rv:11.0)像Gecko

Cookies已启用:true

平台:Win32

用户代理头:Mozilla / 5.0(Windows NT 6.1; WOW64; Trident / 7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4。 0C; .NET4.0E; InfoPath.3; rv:11.0)像Gecko

至少Chrome仍然有一个包含“Chrome”字符串的确切版本号。 但是,对于IE来说,你必须从它所支持的东西中推断出来,以便真正理解它(还有谁会夸耀它们支持.NETMedia Center :P),然后将它与rv:匹配rv:在最后得到版本数。 当然,即使这样复杂的启发式方法很可能会失败,只要IE 12(或者任何他们想要调用它)出来。

PPK的脚本是这种事情的权威,正如@Jalpesh所说,这可能会以正确的方式指向你

 var wn = window.navigator, platform = wn.platform.toString().toLowerCase(), userAgent = wn.userAgent.toLowerCase(), storedName; // ie if (userAgent.indexOf('msie',0) !== -1) { browserName = 'ie'; os = 'win'; storedName = userAgent.match(/msie[ ]\d{1}/).toString(); version = storedName.replace(/msie[ ]/,''); browserOsVersion = browserName + version; } 

采取从https://github.com/leopic/jquery.detectBrowser.js/blob/sans-jquery/jquery.detectBrowser.sansjQuery.js

有一个这样的库: https : //github.com/bestiejs/platform.js#readme

那么你可以这样使用它

 // example 1 platform.os; // 'Windows Server 2008 R2 / 7 x64' // example 2 on an iPad platform.os; // 'iOS 5.0' // you can also access on the browser and some other properties platform.name; // 'Safari' platform.version; // '5.1' platform.product; // 'iPad' platform.manufacturer; // 'Apple' platform.layout; // 'WebKit' // or use the description to put all together platform.description; // 'Safari 5.1 on Apple iPad (iOS 5.0)' 

试试这个..

 // Browser with version Detection navigator.sayswho= (function(){ var N= navigator.appName, ua= navigator.userAgent, tem; var M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i); if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1]; M= M? [M[1], M[2]]: [N, navigator.appVersion,'-?']; return M; })(); var browser_version = navigator.sayswho; alert("Welcome to " + browser_version); 

检查工作小提琴( 这里 )

要使用JavaScript检测操作系统,最好使用navigator.userAgent代替navigator.appVersion

 { var OSName = "Unknown OS"; if (navigator.userAgent.indexOf("Win") != -1) OSName = "Windows"; if (navigator.userAgent.indexOf("Mac") != -1) OSName = "Macintosh"; if (navigator.userAgent.indexOf("Linux") != -1) OSName = "Linux"; if (navigator.userAgent.indexOf("Android") != -1) OSName = "Android"; if (navigator.userAgent.indexOf("like Mac") != -1) OSName = "iOS"; console.log('Your OS: ' + OSName); } 

适用于Firefox,Chrome,Opera,Internet Explorer和Safari

 var ua="Mozilla/1.22 (compatible; MSIE 10.0; Windows 3.1)"; //ua = navigator.userAgent; var b; var browser; if(ua.indexOf("Opera")!=-1) { b=browser="Opera"; } if(ua.indexOf("Firefox")!=-1 && ua.indexOf("Opera")==-1) { b=browser="Firefox"; // Opera may also contains Firefox } if(ua.indexOf("Chrome")!=-1) { b=browser="Chrome"; } if(ua.indexOf("Safari")!=-1 && ua.indexOf("Chrome")==-1) { b=browser="Safari"; // Chrome always contains Safari } if(ua.indexOf("MSIE")!=-1 && (ua.indexOf("Opera")==-1 && ua.indexOf("Trident")==-1)) { b="MSIE"; browser="Internet Explorer"; //user agent with MSIE and Opera or MSIE and Trident may exist. } if(ua.indexOf("Trident")!=-1) { b="Trident"; browser="Internet Explorer"; } // now for version var version=ua.match(b+"[ /]+[0-9]+(.[0-9]+)*")[0]; console.log("broswer",browser); console.log("version",version);