4 jQuery
.client
= new ( function() {
10 /* Public Functions */
13 * Returns an object containing information about the browser
15 * The resulting client object will be in the following format:
17 * 'browser': 'firefox',
22 * 'versionNumber': 3.5,
25 this.profile = function() {
26 // Use the cached version if possible
27 if ( typeof profile
=== 'undefined' ) {
31 // Name of browsers or layout engines we don't recognize
33 // Generic version digit
35 // Strings found in user agent strings that need to be conformed
36 var wildUserAgents
= [ 'Opera', 'Navigator', 'Minefield', 'KHTML', 'Chrome', 'PLAYSTATION 3'];
37 // Translations for conforming user agent strings
38 var userAgentTranslations
= [
39 // Tons of browsers lie about being something they are not
40 [/(Firefox|MSIE|KHTML,\slike\sGecko|Konqueror)/, ''],
41 // Chrome lives in the shadow of Safari still
42 ['Chrome Safari', 'Chrome'],
43 // KHTML is the layout engine not the browser - LIES!
44 ['KHTML', 'Konqueror'],
45 // Firefox nightly builds
46 ['Minefield', 'Firefox'],
47 // This helps keep differnt versions consistent
48 ['Navigator', 'Netscape'],
49 // This prevents version extraction issues, otherwise translation would happen later
50 ['PLAYSTATION 3', 'PS3'],
52 // Strings which precede a version number in a user agent string - combined and used as match 1 in
53 // version detectection
54 var versionPrefixes
= [
55 'camino', 'chrome', 'firefox', 'netscape', 'netscape6', 'opera', 'version', 'konqueror', 'lynx',
56 'msie', 'safari', 'ps3'
58 // Used as matches 2, 3 and 4 in version extraction - 3 is used as actual version number
59 var versionSuffix
= '(\/|\;?\s|)([a-z0-9\.\+]*?)(\;|dev|rel|\\)|\s|$)';
60 // Names of known browsers
62 'camino', 'chrome', 'firefox', 'netscape', 'konqueror', 'lynx', 'msie', 'opera', 'safari', 'ipod',
63 'iphone', 'blackberry', 'ps3'
65 // Tanslations for conforming browser names
66 var browserTranslations
= [];
67 // Names of known layout engines
68 var layoutNames
= ['gecko', 'konqueror', 'msie', 'opera', 'webkit'];
69 // Translations for conforming layout names
70 var layoutTranslations
= [['konqueror', 'khtml'], ['msie', 'trident'], ['opera', 'presto']];
71 // Names of known operating systems
72 var osNames
= ['win', 'mac', 'linux', 'sunos', 'solaris', 'iphone'];
73 // Translations for conforming operating system names
74 var osTranslations
= [['sunos', 'solaris']];
78 // Performs multiple replacements on a string
79 function translate( source
, translations
) {
80 for ( var i
= 0; i
< translations
.length
; i
++ ) {
81 source
= source
.replace( translations
[i
][0], translations
[i
][1] );
88 var userAgent
= navigator
.userAgent
, match
, browser
= uk
, layout
= uk
, os
= uk
, version
= x
;
89 if ( match
= new RegExp( '(' + wildUserAgents
.join( '|' ) + ')' ).exec( userAgent
) ) {
90 // Takes a userAgent string and translates given text into something we can more easily work with
91 userAgent
= translate( userAgent
, userAgentTranslations
);
93 // Everything will be in lowercase from now on
94 userAgent
= userAgent
.toLowerCase();
98 if ( match
= new RegExp( '(' + browserNames
.join( '|' ) + ')' ).exec( userAgent
) ) {
99 browser
= translate( match
[1], browserTranslations
);
101 if ( match
= new RegExp( '(' + layoutNames
.join( '|' ) + ')' ).exec( userAgent
) ) {
102 layout
= translate( match
[1], layoutTranslations
);
104 if ( match
= new RegExp( '(' + osNames
.join( '|' ) + ')' ).exec( navigator
.platform
.toLowerCase() ) ) {
105 var os
= translate( match
[1], osTranslations
);
107 if ( match
= new RegExp( '(' + versionPrefixes
.join( '|' ) + ')' + versionSuffix
).exec( userAgent
) ) {
111 /* Edge Cases -- did I mention about how user agent string lie? */
113 // Decode Safari's crazy 400+ version numbers
114 if ( name
.match( /safari/ ) && version
> 400 ) {
117 // Expose Opera 10's lies about being Opera 9.8
118 if ( name
=== 'opera' && version
>= 9.8) {
119 version
= userAgent
.match( /version\/([0-9\.]*)/i )[1] || 10;
129 'versionBase': ( version
!== x
? new String( version
).substr( 0, 1 ) : x
),
130 'versionNumber': ( parseFloat( version
, 10 ) || 0.0 )
137 * Checks the current browser against a support map object to determine if the browser has been black-listed or
138 * not. If the browser was not configured specifically it is assumed to work. It is assumed that the body
139 * element is classified as either "ltr" or "rtl". If neither is set, "ltr" is assumed.
141 * A browser map is in the following format:
144 * // Multiple rules with configurable operators
145 * 'msie': [['>=', 7], ['!=', 9]],
146 * // Blocked entirely
150 * // Test against a string
151 * 'msie': [['!==', '8.1.2.3']],
152 * // RTL rules do not fall through to LTR rules, you must explicity set each of them
157 * @param map Object of browser support map
159 * @return Boolean true if browser known or assumed to be supported, false if blacklisted
161 this.test = function( map
) {
162 var profile
= jQuery
.client
.profile();
163 var dir
= jQuery( 'body' ).is( '.rtl' ) ? 'rtl' : 'ltr';
164 // Check over each browser condition to determine if we are running in a compatible client
165 if ( typeof map
[dir
] !== 'object' || map
[dir
][profile
.browser
] !== 'object' ) {
166 // Unknown, so we assume it's working
169 var browser
= map
[dir
][profile
.browser
];
170 for ( var condition
in browser
) {
171 var op
= browser
[condition
][0];
172 var val
= browser
[condition
][1];
173 if ( val
=== false ) {
175 } else if ( typeof val
== 'string' ) {
176 if ( !( eval( 'profile.version' + op
+ '"' + val
+ '"' ) ) ) {
179 } else if ( typeof val
== 'number' ) {
180 if ( !( eval( 'profile.versionNumber' + op
+ val
) ) ) {