如何使用 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”版本。

原文由 capri 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 3.7k
2 个回答

检测浏览器的详细信息:

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

原文由 Himanshu 发布,翻译遵循 CC BY-SA 3.0 许可协议

更新

我在 Platform.js 方面有过一些很好的经验( 这里是演示),但仍然建议谨慎行事:

原帖

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

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

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>

铬 说:

浏览器代号:Mozilla

浏览器名称:Netscape

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

启用 Cookie:true

平台:Win32

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

IE 说:

浏览器代号: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;.NET4.0C;.NET4 .0E; InfoPath.3; rv:11.0) 像 Gecko

启用 Cookie: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: 最后获取版本号。当然,一旦 IE 12(或他们想称呼的任何名称)出现,即使如此复杂的启发式方法也很可能会失败。

原文由 Domi 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题