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,
29 // Use the cached version if possible
30 if ( 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 var translate = function( 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,
101 if ( match = new RegExp( '(' + wildUserAgents.join( '|' ) + ')' ).exec( userAgent ) ) {
102 // Takes a userAgent string and translates given text into something we can more easily work with
103 userAgent = translate( userAgent, userAgentTranslations );
105 // Everything will be in lowercase from now on
106 userAgent = userAgent.toLowerCase();
110 if ( match = new RegExp( '(' + names.join( '|' ) + ')' ).exec( userAgent ) ) {
111 name = translate( match[1], nameTranslations );
113 if ( match = new RegExp( '(' + layouts.join( '|' ) + ')' ).exec( userAgent ) ) {
114 layout = translate( match[1], layoutTranslations );
116 if ( match = new RegExp( '(' + layoutVersions.join( '|' ) + ')\\\/(\\d+)').exec( navigator.userAgent.toLowerCase() ) ) {
117 layoutversion = parseInt( match[2], 10 );
119 if ( match = new RegExp( '(' + platforms.join( '|' ) + ')' ).exec( navigator.platform.toLowerCase() ) ) {
120 platform = translate( match[1], platformTranslations );
122 if ( match = new RegExp( '(' + versionPrefixes.join( '|' ) + ')' + versionSuffix ).exec( userAgent ) ) {
126 /* Edge Cases -- did I mention about how user agent string lie? */
128 // Decode Safari's crazy 400+ version numbers
129 if ( name.match( /safari/ ) && version > 400 ) {
132 // Expose Opera 10's lies about being Opera 9.8
133 if ( name === 'opera' && version >= 9.8) {
134 version = userAgent.match( /version\/([0-9\.]*)/i )[1] || 10;
136 var versionNumber = parseFloat( version, 10 ) || 0.0;
143 'layoutVersion': layoutversion,
144 'platform': platform,
146 'versionBase': ( version !== x ? Math.floor( versionNumber ).toString() : x ),
147 'versionNumber': versionNumber
154 * Checks the current browser against a support map object to determine if the browser has been black-listed or
155 * not. If the browser was not configured specifically it is assumed to work. It is assumed that the body
156 * element is classified as either "ltr" or "rtl". If neither is set, "ltr" is assumed.
158 * A browser map is in the following format:
161 * // Multiple rules with configurable operators
162 * 'msie': [['>=', 7], ['!=', 9]],
163 * // Blocked entirely
167 * // Test against a string
168 * 'msie': [['!==', '8.1.2.3']],
169 * // RTL rules do not fall through to LTR rules, you must explicity set each of them
174 * @param map Object of browser support map
176 * @return Boolean true if browser known or assumed to be supported, false if blacklisted
178 test: function( map ) {
179 var profile = $.client.profile();
180 var dir = $( 'body' ).is( '.rtl' ) ? 'rtl' : 'ltr';
181 // Check over each browser condition to determine if we are running in a compatible client
182 if ( typeof map[dir] !== 'object' || typeof map[dir][profile.name] === 'undefined' ) {
183 // Unknown, so we assume it's working
186 var name = map[dir][profile.name];
187 for ( var condition in name ) {
188 var op = name[condition][0];
189 var val = name[condition][1];
190 if ( val === false ) {
192 } else if ( typeof val == 'string' ) {
193 if ( !( eval( 'profile.version' + op + '"' + val + '"' ) ) ) {
196 } else if ( typeof val == 'number' ) {
197 if ( !( eval( 'profile.versionNumber' + op + val ) ) ) {
206 $( document ).ready( function() {
207 var profile = $.client.profile();
209 .addClass( 'client-' + profile.name )
210 .addClass( 'client-' + profile.name + '-' + profile.versionBase )
211 .addClass( 'client-' + profile.layout )
212 .addClass( 'client-' + profile.platform );