猿问

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

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


泛舟湖上清波郎朗
浏览 2131回答 3
3回答

青春有我

检测浏览器的详细信息:var&nbsp;nVer&nbsp;=&nbsp;navigator.appVersion;var&nbsp;nAgt&nbsp;=&nbsp;navigator.userAgent;var&nbsp;browserName&nbsp;&nbsp;=&nbsp;navigator.appName;var&nbsp;fullVersion&nbsp;&nbsp;=&nbsp;'' +parseFloat(navigator.appVersion);&nbsp;var&nbsp;majorVersion&nbsp;=&nbsp;parseInt(navigator.appVersion,10);var&nbsp;nameOffset,verOffset,ix; //&nbsp;In&nbsp;Opera,&nbsp;the&nbsp;true&nbsp;version&nbsp;is&nbsp;after&nbsp;"Opera"&nbsp;or&nbsp;after&nbsp;"Version"if&nbsp;((verOffset=nAgt.indexOf("Opera"))!=-1)&nbsp;{ &nbsp;browserName&nbsp;=&nbsp;"Opera"; &nbsp;fullVersion&nbsp;=&nbsp;nAgt.substring(verOffset+6); &nbsp;if&nbsp;((verOffset=nAgt.indexOf("Version"))!=-1)&nbsp; &nbsp;&nbsp;&nbsp;fullVersion&nbsp;=&nbsp;nAgt.substring(verOffset+8);} &nbsp;&nbsp;&nbsp;//&nbsp;In&nbsp;MSIE,&nbsp;the&nbsp;true&nbsp;version&nbsp;is&nbsp;after&nbsp;"MSIE"&nbsp;in&nbsp;userAgentelse&nbsp;if&nbsp;((verOffset=nAgt.indexOf("MSIE"))!=-1)&nbsp;{ &nbsp;browserName&nbsp;=&nbsp;"Microsoft&nbsp;Internet&nbsp;Explorer"; &nbsp;fullVersion&nbsp;=&nbsp;nAgt.substring(verOffset+5);} &nbsp;//&nbsp;In&nbsp;Chrome,&nbsp;the&nbsp;true&nbsp;version&nbsp;is&nbsp;after&nbsp;"Chrome"&nbsp;else&nbsp;if&nbsp;((verOffset=nAgt.indexOf("Chrome"))!=-1)&nbsp;{ &nbsp;browserName&nbsp;=&nbsp;"Chrome"; &nbsp;fullVersion&nbsp;=&nbsp;nAgt.substring(verOffset+7);} &nbsp;//&nbsp;In&nbsp;Safari,&nbsp;the&nbsp;true&nbsp;version&nbsp;is&nbsp;after&nbsp;"Safari"&nbsp;or&nbsp;after&nbsp;"Version"&nbsp;else&nbsp;if&nbsp;((verOffset=nAgt.indexOf("Safari"))!=-1)&nbsp;{ &nbsp;browserName&nbsp;=&nbsp;"Safari"; &nbsp;fullVersion&nbsp;=&nbsp;nAgt.substring(verOffset+7); &nbsp;if&nbsp;((verOffset=nAgt.indexOf("Version"))!=-1)&nbsp; &nbsp;&nbsp;&nbsp;fullVersion&nbsp;=&nbsp;nAgt.substring(verOffset+8);} &nbsp;&nbsp;&nbsp;//&nbsp;In&nbsp;Firefox,&nbsp;the&nbsp;true&nbsp;version&nbsp;is&nbsp;after&nbsp;"Firefox"&nbsp;else&nbsp;if&nbsp;((verOffset=nAgt.indexOf("Firefox"))!=-1)&nbsp;{ &nbsp;browserName&nbsp;=&nbsp;"Firefox"; &nbsp;fullVersion&nbsp;=&nbsp;nAgt.substring(verOffset+8);} &nbsp;//&nbsp;In&nbsp;most&nbsp;other&nbsp;browsers,&nbsp;"name/version"&nbsp;is&nbsp;at&nbsp;the&nbsp;end&nbsp;of&nbsp;userAgent&nbsp;else&nbsp;if&nbsp;(&nbsp;(nameOffset=nAgt.lastIndexOf('&nbsp;')+1)&nbsp;<&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(verOffset=nAgt.lastIndexOf('/'))&nbsp;)&nbsp;{ &nbsp;browserName&nbsp;=&nbsp;nAgt.substring(nameOffset,verOffset); &nbsp;fullVersion&nbsp;=&nbsp;nAgt.substring(verOffset+1); &nbsp;if&nbsp;(browserName.toLowerCase()==browserName.toUpperCase())&nbsp;{ &nbsp;&nbsp;browserName&nbsp;=&nbsp;navigator.appName; &nbsp;}}//&nbsp;trim&nbsp;the&nbsp;fullVersion&nbsp;string&nbsp;at&nbsp;semicolon/space&nbsp;if&nbsp;presentif&nbsp;((ix=fullVersion.indexOf(";"))!=-1) &nbsp;&nbsp;&nbsp;fullVersion=fullVersion.substring(0,ix);if&nbsp;((ix=fullVersion.indexOf("&nbsp;"))!=-1) &nbsp;&nbsp;&nbsp;fullVersion=fullVersion.substring(0,ix);majorVersion&nbsp;=&nbsp;parseInt(''+fullVersion,10);if&nbsp;(isNaN(majorVersion))&nbsp;{ &nbsp;fullVersion&nbsp;&nbsp;=&nbsp;''+parseFloat(navigator.appVersion);&nbsp; &nbsp;majorVersion&nbsp;=&nbsp;parseInt(navigator.appVersion,10);}document.write('' &nbsp;+'Browser&nbsp;name&nbsp;&nbsp;=&nbsp;'+browserName+'<br>' &nbsp;+'Full&nbsp;version&nbsp;&nbsp;=&nbsp;'+fullVersion+'<br>' &nbsp;+'Major&nbsp;version&nbsp;=&nbsp;'+majorVersion+'<br>' &nbsp;+'navigator.appName&nbsp;=&nbsp;'+navigator.appName+'<br>' &nbsp;+'navigator.userAgent&nbsp;=&nbsp;'+navigator.userAgent+'<br>')来源JavaScript:浏览器名.请参阅JSFiddle以检测浏览器详细信息。.检测操作系统://&nbsp;This&nbsp;script&nbsp;sets&nbsp;OSName&nbsp;variable&nbsp;as&nbsp;follows: //&nbsp;"Windows"&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;all&nbsp;versions&nbsp;of&nbsp;Windows //&nbsp;"MacOS"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;all&nbsp;versions&nbsp;of&nbsp;Macintosh&nbsp;OS //&nbsp;"Linux"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;all&nbsp;versions&nbsp;of&nbsp;Linux //&nbsp;"UNIX"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;all&nbsp;other&nbsp;UNIX&nbsp;flavors&nbsp; //&nbsp;"Unknown&nbsp;OS"&nbsp;indicates&nbsp;failure&nbsp;to&nbsp;detect&nbsp;the&nbsp;OSvar&nbsp;OSName="Unknown&nbsp;OS"; if&nbsp;(navigator.appVersion.indexOf("Win")!=-1)&nbsp;OSName="Windows"; if&nbsp;(navigator.appVersion.indexOf("Mac")!=-1)&nbsp;OSName="MacOS"; if&nbsp;(navigator.appVersion.indexOf("X11")!=-1)&nbsp;OSName="UNIX"; if&nbsp;(navigator.appVersion.indexOf("Linux")!=-1)&nbsp;OSName="Linux"; document.write('Your&nbsp;OS:&nbsp;'+OSName);来源JavaScript:OS检测.请参阅JSFiddle以检测OS详细信息.

宝慕林4294392

我很遗憾地说:我们在这件事上运气不佳。我想请你参阅WhichBrowser:&nbsp;每个人都在撒谎.基本上,没有浏览器是诚实的。不管你使用的是Chrome还是IE,它们都会告诉你,它们是带有壁虎和Safari支持的“Mozilla Netscape”。你自己试试,在这条线上飞来走去的任何一个小提琴:他的小提琴Hariharan小提琴或者其他任何.。用Chrome试试(它可能还能成功),然后用IE的最新版本尝试它,你会哭的。当然,也有一些启发式方法,可以很好地解决问题,但是要掌握所有的边缘情况将是很乏味的,而且在一年的时间里,它们很可能不再起作用了。以您的代码为例:<div&nbsp;id="example"></div><script&nbsp;type="text/javascript">txt&nbsp;=&nbsp;"<p>Browser&nbsp;CodeName:&nbsp;"&nbsp;+&nbsp;navigator.appCodeName&nbsp;+&nbsp;"</p>"; txt+=&nbsp;"<p>Browser&nbsp;Name:&nbsp;"&nbsp;+&nbsp;navigator.appName&nbsp;+&nbsp;"</p>";txt+=&nbsp;"<p>Browser&nbsp;Version:&nbsp;"&nbsp;+&nbsp;navigator.appVersion&nbsp;+&nbsp;"</p>"; txt+=&nbsp;"<p>Cookies&nbsp;Enabled:&nbsp;"&nbsp;+&nbsp;navigator.cookieEnabled&nbsp;+&nbsp;"</p>";txt+=&nbsp;"<p>Platform:&nbsp;"&nbsp;+&nbsp;navigator.platform&nbsp;+&nbsp;"</p>"; txt+=&nbsp;"<p>User-agent&nbsp;header:&nbsp;"&nbsp;+&nbsp;navigator.userAgent&nbsp;+&nbsp;"</p>";document.getElementById("example").innerHTML=txt;</script>Chrome说:浏览器代号:Mozilla浏览器名称:Netscape浏览器版本:5.0(WindowsNT6.1;WOW 64)AppleWebKit/537.36(KHTML,类似壁虎)Chrome/40.2214.115 Safari/537.36启用Cookie:真平台:Win 32用户代理标题:Mozilla/5.0(WindowsNT6.1;WOW 64)AppleWebKit/537.36(KHTML,类似壁虎)Chrome/40.0.2214.115 Safari/537.36IE说:浏览器代号:Mozilla浏览器名称:Netscape浏览器版本:5.0(Windows NT 6.1;WOW 64;Tridin/7.0;SLCC 2;.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)启用Cookie:真平台:Win 32用户代理报头:Mozilla/5.0(Windows NT 6.1;WOW 64;Tridin/7.0;SLCC 2;.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)至少Chrome仍然有一个字符串,其中包含有确切版本号的“Chrome”。但是,对于IE,你必须从它所支持的东西中推断出来,才能真正弄清楚它(还有谁会夸口说他们支持它呢?).NET或Media Center:p),然后将其与rv:最后得到版本号。当然,即使是这样复杂的启发式方法也很可能在IE12(或他们想称之为它的任何东西)一出来就失败。
随时随地看视频慕课网APP
我要回答