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