6 $.client
= new ( function() {
12 /* Public Functions */
15 * Returns an object containing information about the browser
17 * The resulting client object will be in the following format:
21 * 'layoutVersion': '20101026',
25 * 'versionNumber': 3.5,
28 this.profile = function() {
29 // Use the cached version if possible
30 if ( typeof profile
=== 'undefined' ) {
34 // Name of browsers or layout engines we don't recognize
36 // Generic version digit
38 // Strings found in user agent strings that need to be conformed
39 var wildUserAgents
= [ 'Opera', 'Navigator', 'Minefield', 'KHTML', 'Chrome', 'PLAYSTATION 3'];
40 // Translations for conforming user agent strings
41 var userAgentTranslations
= [
42 // Tons of browsers lie about being something they are not
43 [/(Firefox|MSIE|KHTML,\slike\sGecko|Konqueror)/, ''],
44 // Chrome lives in the shadow of Safari still
45 ['Chrome Safari', 'Chrome'],
46 // KHTML is the layout engine not the browser - LIES!
47 ['KHTML', 'Konqueror'],
48 // Firefox nightly builds
49 ['Minefield', 'Firefox'],
50 // This helps keep differnt versions consistent
51 ['Navigator', 'Netscape'],
52 // This prevents version extraction issues, otherwise translation would happen later
53 ['PLAYSTATION 3', 'PS3'],
55 // Strings which precede a version number in a user agent string - combined and used as match 1 in
56 // version detectection
57 var versionPrefixes
= [
58 'camino', 'chrome', 'firefox', 'netscape', 'netscape6', 'opera', 'version', 'konqueror', 'lynx',
59 'msie', 'safari', 'ps3'
61 // Used as matches 2, 3 and 4 in version extraction - 3 is used as actual version number
62 var versionSuffix
= '(\\/|\\;?\\s|)([a-z0-9\\.\\+]*?)(\\;|dev|rel|\\)|\\s|$)';
63 // Names of known browsers
65 'camino', 'chrome', 'firefox', 'netscape', 'konqueror', 'lynx', 'msie', 'opera', 'safari', 'ipod',
66 'iphone', 'blackberry', 'ps3'
68 // Tanslations for conforming browser names
69 var nameTranslations
= [];
70 // Names of known layout engines
71 var layouts
= ['gecko', 'konqueror', 'msie', 'opera', 'webkit'];
72 // Translations for conforming layout names
73 var layoutTranslations
= [['konqueror', 'khtml'], ['msie', 'trident'], ['opera', 'presto']];
74 // Names of supported layout engines for version number
75 var layoutVersions
= ['applewebkit', 'gecko'];
76 // Names of known operating systems
77 var platforms
= ['win', 'mac', 'linux', 'sunos', 'solaris', 'iphone'];
78 // Translations for conforming operating system names
79 var platformTranslations
= [['sunos', 'solaris']];
83 // Performs multiple replacements on a string
84 function translate( source
, translations
) {
85 for ( var i
= 0; i
< translations
.length
; i
++ ) {
86 source
= source
.replace( translations
[i
][0], translations
[i
][1] );
93 var userAgent
= navigator
.userAgent
, match
, name
= uk
, layout
= uk
, layoutversion
= uk
, platform
= uk
, version
= x
;
94 if ( match
= new RegExp( '(' + wildUserAgents
.join( '|' ) + ')' ).exec( userAgent
) ) {
95 // Takes a userAgent string and translates given text into something we can more easily work with
96 userAgent
= translate( userAgent
, userAgentTranslations
);
98 // Everything will be in lowercase from now on
99 userAgent
= userAgent
.toLowerCase();
103 if ( match
= new RegExp( '(' + names
.join( '|' ) + ')' ).exec( userAgent
) ) {
104 name
= translate( match
[1], nameTranslations
);
106 if ( match
= new RegExp( '(' + layouts
.join( '|' ) + ')' ).exec( userAgent
) ) {
107 layout
= translate( match
[1], layoutTranslations
);
109 if ( match
= new RegExp( '(' + layoutVersions
.join( '|' ) + ')\\\/(\\d+)').exec( navigator
.userAgent
.toLowerCase() ) ) {
110 layoutversion
= parseInt(match
[2]);
112 if ( match
= new RegExp( '(' + platforms
.join( '|' ) + ')' ).exec( navigator
.platform
.toLowerCase() ) ) {
113 platform
= translate( match
[1], platformTranslations
);
115 if ( match
= new RegExp( '(' + versionPrefixes
.join( '|' ) + ')' + versionSuffix
).exec( userAgent
) ) {
119 /* Edge Cases -- did I mention about how user agent string lie? */
121 // Decode Safari's crazy 400+ version numbers
122 if ( name
.match( /safari/ ) && version
> 400 ) {
125 // Expose Opera 10's lies about being Opera 9.8
126 if ( name
=== 'opera' && version
>= 9.8) {
127 version
= userAgent
.match( /version\/([0-9\.]*)/i )[1] || 10;
135 'layoutVersion': layoutversion
,
136 'platform': platform
,
138 'versionBase': ( version
!== x
? new String( version
).substr( 0, 1 ) : x
),
139 'versionNumber': ( parseFloat( version
, 10 ) || 0.0 )
146 * Checks the current browser against a support map object to determine if the browser has been black-listed or
147 * not. If the browser was not configured specifically it is assumed to work. It is assumed that the body
148 * element is classified as either "ltr" or "rtl". If neither is set, "ltr" is assumed.
150 * A browser map is in the following format:
153 * // Multiple rules with configurable operators
154 * 'msie': [['>=', 7], ['!=', 9]],
155 * // Blocked entirely
159 * // Test against a string
160 * 'msie': [['!==', '8.1.2.3']],
161 * // RTL rules do not fall through to LTR rules, you must explicity set each of them
166 * @param map Object of browser support map
168 * @return Boolean true if browser known or assumed to be supported, false if blacklisted
170 this.test = function( map
) {
171 var profile
= $.client
.profile();
172 var dir
= $( 'body' ).is( '.rtl' ) ? 'rtl' : 'ltr';
173 // Check over each browser condition to determine if we are running in a compatible client
174 if ( typeof map
[dir
] !== 'object' || typeof map
[dir
][profile
.name
] === 'undefined' ) {
175 // Unknown, so we assume it's working
178 var name
= map
[dir
][profile
.name
];
179 for ( var condition
in name
) {
180 var op
= name
[condition
][0];
181 var val
= name
[condition
][1];
182 if ( val
=== false ) {
184 } else if ( typeof val
== 'string' ) {
185 if ( !( eval( 'profile.version' + op
+ '"' + val
+ '"' ) ) ) {
188 } else if ( typeof val
== 'number' ) {
189 if ( !( eval( 'profile.versionNumber' + op
+ val
) ) ) {
198 $( document
).ready( function() {
199 var profile
= $.client
.profile();
201 .addClass( 'client-' + profile
.name
)
202 .addClass( 'client-' + profile
.name
+ '-' + profile
.versionBase
)
203 .addClass( 'client-' + profile
.layout
)
204 .addClass( 'client-' + profile
.platform
);