window.navigator 对象包含有关访问者浏览器的信息。浏览器
window.navigator 对象在编写时可不使用 window 这个前缀。cookie
<div id="example"></div> <script> 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>"; txt+= "<p>User-agent language: " + navigator.systemLanguage + "</p>"; document.getElementById("example").innerHTML=txt; </script>
警告:来自 navigator 对象的信息具备误导性,不该该被用于检测浏览器版本,这是由于:app
因为 navigator 可误导浏览器检测,使用对象检测可用来嗅探不一样的浏览器。操作系统
因为不一样的浏览器支持不一样的对象,您能够使用对象来检测浏览器。例如,因为只有 Opera 支持属性 "window.opera",您能够据此识别出 Opera。code
例子:orm
if (window.opera) {...some action...}