The script

Copy this script into your JavaScript files. It works immediately, and you can query three properties of the BrowserDetect object:

//Browser name:
BrowserDetect.browser//Browser version:
BrowserDetect.version//OS name:
BrowserDetect.OS

代码:

var BrowserDetect = {init: function () {this.browser = this.searchString(this.dataBrowser) || "An unknown browser";this.version = this.searchVersion(navigator.userAgent)|| this.searchVersion(navigator.appVersion)|| "an unknown version";this.OS = this.searchString(this.dataOS) || "an unknown OS";},searchString: function (data) {for (var i=0;i<data.length;i++)   {var dataString = data[i].string;var dataProp = data[i].prop;this.versionSearchString = data[i].versionSearch || data[i].identity;if (dataString) {if (dataString.indexOf(data[i].subString) != -1)return data[i].identity;}else if (dataProp)return data[i].identity;}},searchVersion: function (dataString) {var index = dataString.indexOf(this.versionSearchString);if (index == -1) return;return parseFloat(dataString.substring(index+this.versionSearchString.length+1));},dataBrowser: [{string: navigator.userAgent,subString: "Chrome",identity: "Chrome"},{    string: navigator.userAgent,subString: "OmniWeb",versionSearch: "OmniWeb/",identity: "OmniWeb"},{string: navigator.vendor,subString: "Apple",identity: "Safari",versionSearch: "Version"},{prop: window.opera,identity: "Opera",versionSearch: "Version"},{string: navigator.vendor,subString: "iCab",identity: "iCab"},{string: navigator.vendor,subString: "KDE",identity: "Konqueror"},{string: navigator.userAgent,subString: "Firefox",identity: "Firefox"},{string: navigator.vendor,subString: "Camino",identity: "Camino"},{        // for newer Netscapes (6+)string: navigator.userAgent,subString: "Netscape",identity: "Netscape"},{string: navigator.userAgent,subString: "MSIE",identity: "Explorer",versionSearch: "MSIE"},{string: navigator.userAgent,subString: "Gecko",identity: "Mozilla",versionSearch: "rv"},{       // for older Netscapes (4-)string: navigator.userAgent,subString: "Mozilla",identity: "Netscape",versionSearch: "Mozilla"}],dataOS : [{string: navigator.platform,subString: "Win",identity: "Windows"},{string: navigator.platform,subString: "Mac",identity: "Mac"},{string: navigator.userAgent,subString: "iPhone",identity: "iPhone/iPod"},{string: navigator.platform,subString: "Linux",identity: "Linux"}]};
BrowserDetect.init();//alert(BrowserDetect.browser);

This script will only continue to work if you regularly check whether newer browsers still follow the rules set forth in the dataBrowser and dataOS arrays.

Browser detection

The dataBrowser array is filled with objects that contain the properties that help the script detect your users' browser. Note its general syntax:

dataBrowser: [{prop: window.opera,identity: "Opera",versionSearch: "Version" // note: no comma},{string: navigator.userAgent,subString: "MSIE",identity: "Explorer",versionSearch: "MSIE" // note: no comma} // note: no comma
];

The [] define an array literal, and all of its elements are object literals. Each object literal is enclosed in curly braces {} and contains a few properties (name: value,). Note that a comma between the objects and between the properties is required, but that the last comma is always forbidden.

Properties

Every object in the dataBrowser array can contain the following properties:

  1. string and subString properties. These say: "search for subString in string". If the subString is found, the browser is identified.
  2. a prop property. It says "see if prop is supported". If it is, the browser is identified.
  3. an identity string. This string becomes the value of BrowserDetect.browser.
  4. a versionSearch string. This is for searching for the version number (see below). If this property is absent, the identity string is used instead.

Every object must contain either 1 or 2 (never both!), must contain 3 and may contain 4.

Example: Safari

As an example, here's the Safari object:

{string: navigator.vendor,subString: "Apple",identity: "Safari"
},

The script takes the value of navigator.vendor and sees if it contains the string "Apple". If it does, BrowserDetect.browser is set to "Safari" and the browser detection quits. If it doesn't, the script moves on to the next object.

Example: Opera

The next object is Opera:

{prop: window.opera,identity: "Opera",versionSearch: "Version"
},

Here the script checks if the property window.opera exists. If it does, BrowserDetect.browser is set to "Opera". If it doesn't, the script moves on to the next object.

If the browser turns out to be Opera the script looks for version information after the "Version" string.

userAgent and vendor

The trick of browser detection is knowing where to look for the information you need. Traditionally we use navigator.userAgent. However, precisely because this check is traditional many minor browsers change their navigator.userAgent string so that bad detects written by amateur web developers are fooled into identifying it as Explorer. Section 3D of the book discusses this problem, as well as some gory details of navigator.userAgent.

More recently, new browsers have started to support the navigator.vendor property, which contains information about the vendor. In general I prefer to use this string for my detection, since it's less contaminated by obfuscation.

Of course, as soon as amateurs start using my detection script to detect Safari, Opera, Konqueror or other browsers, the browser vendors will be forced to change the value of navigator.vendor and my detect will not work any more.

Detection order

The objects in dataBrowser are used in the order they appear; that's why dataBrowser is an array. As soon as a positive identification is made the script ends, and it doesn't check the remaining objects.

Detection order is very important. The general rule is that you check for the minor browsers first. The reason is that many minor browsers give their users the opportunity to change identity in order to work around browser detects.

For instance, the Opera navigator.userAgent may contain "MSIE". If we'd check for Explorer first, we'd find the "MSIE" and would incorrectly conclude that the browser is Explorer. In order to avoid this false detection, we should check for Opera first. If the browser is in fact Opera, the script never proceeds to the "MSIE" check.

Safari's navigator.userAgent also contains "Gecko". This causes the same problem: a check for Mozilla would reveal "Gecko", and Safari would be identified as Mozilla. Therefore the Mozilla check only takes place if the browser is not Safari.

If you add a new object to detect a new browser, always add it before the Explorer/Mozilla objects at the end of the array.

Version number

In general, the version number of a browser can be found directly after its name in navigator.userAgent. The script searches for this name and takes the number that appears after it. For instance, this is Konqueror's navigator.userAgent:

Mozilla/5.0 (compatible; Konqueror/3; Linux)

The script searches for the string "Konqueror", skips the next character, and takes the number after that. This is the version number. The script uses parseFloat, so that decimal places in the version number also become part of BrowserDetect.version.

Unfortunately Safari's string never contains its official version; only its internal Apple version number (ie. not 1.3.2 but 312.6). Therefore the version number detect doesn't work in Safari. Since this is clearly Apple's fault (it doesn't follow established conventions), I don't care.

versionSearch

In general, the browser name as it appears in navigator.userAgent is the same as the identification string. If the browser is "iCab", the script searches for "iCab".

However, Explorer needs "MSIE", Mozilla needs "rv", and older Netscape versions need "Mozilla". In order to accomodate these problems you may add a versionSearch property to the browser object. If it's there it's used for the version detect; if it's not there the identity string is used instead.

Take the Firefox and Explorer objects:

{string: navigator.userAgent,subString: "Firefox",identity: "Firefox"
},
{string: navigator.userAgent,subString: "MSIE",identity: "Explorer",versionSearch: "MSIE"
},

If the browser is Firefox, the script should look for the "Firefox" string. Since this is also the browser identity string, a special versionSearch is not necessary.

Explorer, however, puts its version number after the string "MSIE". Since I use "Explorer" as identity string, I have to define the versionSearch property as "MSIE".

userAgent and appVersion

The version detect script searches for the browser name in both navigator.userAgent and navigator.appVersion. The reason is iCab: this browser's navigator.userAgent may not contain the string "iCab", but navigator.appVersion always does.

Operating system

The OS detect works the same as the browser detect. Currently all my OS detects use navigator.platform, since this property appears to always contain the correct information.

navigator

Below you see the objects contained by the object navigator. These variables can be read out and give information about the browser and computer of your users. Use this information to add new objects to the browser detect.

navigator.userAgent = Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:20.0) Gecko/20100101 Firefox/20.0
navigator.vendor =
navigator.platform = MacIntel
navigator.appCodeName = Mozilla
navigator.appName = Netscape
navigator.appVersion = 5.0 (Macintosh)
navigator.language = en-US
navigator.mimeTypes = [object MimeTypeArray]
navigator.oscpu = Intel Mac OS X 10.6
navigator.vendorSub =
navigator.product = Gecko
navigator.productSub = 20100101
navigator.plugins = [object PluginArray]
navigator.cookieEnabled = true
navigator.onLine = true
navigator.buildID = 20130326150557
navigator.doNotTrack = unspecified
navigator.mozPower = null
navigator.javaEnabled = function javaEnabled() {[native code]
}
navigator.taintEnabled = function taintEnabled() {[native code]
}
navigator.vibrate = function vibrate() {[native code]
}
navigator.addIdleObserver = function addIdleObserver() {[native code]
}
navigator.removeIdleObserver = function removeIdleObserver() {[native code]
}
navigator.requestWakeLock = function requestWakeLock() {[native code]
}
navigator.getDeviceStorage = function getDeviceStorage() {[native code]
}
navigator.geolocation = [object GeoGeolocation]
navigator.registerContentHandler = function registerContentHandler() {[native code]
}
navigator.registerProtocolHandler = function registerProtocolHandler() {[native code]
}
navigator.mozIsLocallyAvailable = function mozIsLocallyAvailable() {[native code]
}
navigator.battery = [object BatteryManager]
navigator.mozSms = null
navigator.mozGetUserMediaDevices = function mozGetUserMediaDevices() {[native code]
}
navigator.mozGetUserMedia = function mozGetUserMedia() {[native code]
}
navigator.mozConnection = [object MozConnection]
navigator.mozCameras = null

from: http://www.quirksmode.org/js/detect.html

关联:

1.判断来访者所用设备是iPhone、iPad或者电脑(PC)

2. php 检测浏览器版本和操作系统

3. PHP 判断用户语言跳转网页

4. PHP判断浏览器类型和浏览器语言(附各国语言简写代码)

JavaScript判断浏览器 Browser detect相关推荐

  1. html判断是否在微信里打开,JavaScript判断浏览器内核,微信打开自动提示在浏览器打开...

    微信会屏蔽 URL 自定义的 scheme ,导致无法跳转手机中的浏览器.网上有一些工具类网站可以实现直接跳转浏览器,之后有机会我会整理一下.我们今天只讨论通过 JavaScript 判断是否在微信浏 ...

  2. javascript判断浏览器和终端类型,js如何区分手机、电脑终端和浏览器

    判断浏览器类型 复制代码代码如下: if ( window.sidebar && "object" == typeof( window.sidebar ) & ...

  3. JavaScript判断浏览器内核,微信打开自动提示在浏览器打开

    微信会屏蔽 URL 自定义的 scheme ,导致无法跳转手机中的浏览器.网上有一些工具类网站可以实现直接跳转浏览器,之后有机会我会整理一下.我们今天只讨论通过 JavaScript 判断是否在微信浏 ...

  4. JavaScript判断浏览器类型及版本

    JavaScript判断浏览器类型及版本 你知道世界上有多少种浏览器吗?除了我们熟知的IE, Firefox, Opera, Safari四大浏览器之外,世界上还有近百种浏览器. 几天前,浏览器家族有 ...

  5. JavaScript判断浏览器类型及版本(新增谷歌的Chrome)

    来源:http://blog.tripdev.com/?tid=164 JavaScript是前端开发的主要语言,我们可以通过编写JavaScript程序来判断浏览器的类型及版本.JavaScript ...

  6. JavaScript判断浏览器Flash Player信息

    今天研究了点Flex技术,做了一个小的Demo,在测试时发现经常报错,网上一查发现是浏览器Flash Player版本较低造成(需要10及其以上的版本)的,对此总结了一下借助JavaScript脚本判 ...

  7. javascript 判断浏览器

    为什么80%的码农都做不了架构师?>>>    navigator.userAgent 通常我们可以通过navigator.userAgent只读属性来获取浏览器的一些信息,算是原生 ...

  8. javascript判断浏览器核心

    20 21 22 23 24 /** * 判断浏览器核心 * @return IE6.0/IE7.0/IE8.0/FireFox/Opera/other * @author ypz */ functi ...

  9. 【转】javascript判断浏览器是不是IE

    一个项目中要判断浏览器是不是IE6,用jQuery的utility里面的方法$.browser.version判断居然总是显示是IE6,但我用的明明是IE7(我的操作系统是server2003,$.b ...

最新文章

  1. 大厂不一定要进,算法必须要学!精选算法文章 89 篇
  2. Wireshark非标准分析port无流量
  3. 我的世界服务器如何修改加载规模,我的世界服务器预加载区域怎么调小
  4. maven 打包时缺少文件_解决Intellij Idea下使用Maven项目打包时部分文件缺失问题
  5. $@ $# $2 $0 $* Linux 参数使用
  6. python求解分支定界(branch-and-bound)问题使用pybnb基本架构
  7. java 分库关联查询工具类
  8. matlab 可视化 —— 常用绘图函数
  9. springboot+activiti工作流mybatis冲突解决办法
  10. Error:Execution failed for task ':app:clean'. Unable to delete directory: /media/file/workspaces/a
  11. 纽微特记事:可笑的国际版
  12. MongoVUE 使用教程
  13. PNG隐写入门赛 WP
  14. shipyard安装不迷茫
  15. ios 不能触发click事件
  16. python正则表达式入门
  17. 马斯克打了个响指,推特50%员工被裁....
  18. HIFI音箱中最常用的七种音箱摆位方法
  19. 手把手教你 2020 年退税申报,学生党、工作党都有
  20. 【STM32F407的DSP教程】第33章 STM32F407不限制点数FFT实现

热门文章

  1. 计算星期几(信息学奥赛一本通-T1083)
  2. 信息学奥赛C++语言:短信计费
  3. 信息学奥赛一本通C++语言——1090:含k个3的数
  4. 35 SD配置-销售凭证设置-定义项目类别组
  5. 书籍《智能交通》-观后感-2021年12月-下期分享
  6. python代码规范准则_Python编码规范
  7. z01、z02.....怎么解压缩
  8. 电脑显卡接口类型:VGA、HDMI、DP
  9. [有限元] Ansys Workbench Mechanical 中的应力应变显示类型的文档翻译
  10. LinkedList理解(1)结构