从长久来看,这样有利于在复杂情况下根据浏览器特性进行分别处理, 而不是简单的检测浏览器类型和版本。 但目前很多旧程序的移植恐怕无法直接过渡为根据浏览器支持特性, 所以在网上找了一些能够直接替换的解决办法。
判断浏览器类型:
等号后面的表达式返回的就是 true/false, 可以直接用来替换原来的$.browser.msie 等。
检查是否为 IE6:
检查是否为 IE 6-8:
终极方法是用另外的类库替代,比如 这个 , 但作者也不推荐使用浏览器类型和版本来进行判断。
来自 http://www.fwolf.com/blog/post/35
Browser detect
A useful but often overrated JavaScript function is the browser detect. Sometimes you want to give specific instructions or load a new page in case the viewer uses, for instance, Safari.
If you're new to JavaScript, don't use browser detects. You don’t need them. Please read the object detection page first.
Use WhichBrowser
This page used to contain my own browser detect script, but I found that I do not have enough time to keep it up to date. Therefore I removed it.
I advise you to use WhichBrowser. It’s up-to-date, and contains a treasure trove of mobile information.
If you use my old script I advise you to switch to WhichBrowser.
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.
navigator.vendorSub = navigator.productSub = 20030107 navigator.vendor = Google Inc. navigator.maxTouchPoints = 0 navigator.hardwareConcurrency = 2 navigator.appCodeName = Mozilla navigator.appName = Netscape navigator.appVersion = 5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.8 Safari/537.36 navigator.platform = Win32 navigator.product = Gecko navigator.userAgent = Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.8 Safari/537.36 navigator.language = zh-CN navigator.languages = zh-CN,zh navigator.onLine = true navigator.cookieEnabled = true navigator.getStorageUpdates = function getStorageUpdates() { [native code] } navigator.doNotTrack = null navigator.geolocation = [object Geolocation] navigator.plugins = [object PluginArray] navigator.mimeTypes = [object MimeTypeArray] navigator.webkitTemporaryStorage = [object DeprecatedStorageQuota] navigator.webkitPersistentStorage = [object DeprecatedStorageQuota] navigator.serviceWorker = [object ServiceWorkerContainer] navigator.getBattery = function getBattery() { [native code] } navigator.sendBeacon = function sendBeacon() { [native code] } navigator.getGamepads = function getGamepads() { [native code] } navigator.webkitGetUserMedia = function webkitGetUserMedia() { [native code] } navigator.javaEnabled = function javaEnabled() { [native code] } navigator.vibrate = function vibrate() { [native code] } navigator.requestMIDIAccess = function requestMIDIAccess() { [native code] } navigator.mediaDevices = [object MediaDevices] navigator.permissions = [object Permissions] navigator.presentation = [object Presentation] navigator.requestMediaKeySystemAccess = function requestMediaKeySystemAccess() { [native code] } navigator.registerProtocolHandler = function registerProtocolHandler() { [native code] } navigator.unregisterProtocolHandler = function unregisterProtocolHandler() { [native code] } 来自 http://www.quirksmode.org/js/detect.html