1/* 2 * platformSelector: a jQuery plugin 3 * 4 * For full documentation, visit: 5 * http://github.com/alexrabarts/jquery-platformselector 6 * 7 * Licensed under the MIT: 8 * http://www.opensource.org/licenses/mit-license.php 9 * 10 */ 11 12(function ($) { 13 $.extend($, { 14 platformSelector: function (options) { 15 options = options || {userAgent: navigator.userAgent}; 16 17 function uaIs(test) { return ua.indexOf(test) !== -1; } 18 19 function isMobile() { 20 var knownMobileDevices = [ 21 'mobile', 22 'ipod', 23 'iphone', 24 'ipad', 25 'android', 26 'blackberry', 27 'webos', 28 'symbian', 29 'windows phone', 30 'smartphone', 31 'j2me' 32 ]; 33 34 var mobile = false; 35 36 $.each(knownMobileDevices, function () { 37 if (uaIs(this)) { 38 mobile = true; 39 return false; 40 } 41 }); 42 43 return mobile; 44 } 45 46 var ua = options.userAgent.toLowerCase(); 47 48 var ie = ( 49 !uaIs('opera') && 50 !uaIs('webtv') && 51 (/msie (\d+)/.test(ua)) 52 ) ? 'ie ie_' + RegExp.$1 : ''; 53 54 var os = 55 uaIs('ipod') ? 'ios ipod' : 56 uaIs('iphone') ? 'ios iphone' : 57 uaIs('ipad') ? 'ios ipad' : 58 uaIs('android') ? 'android' : 59 uaIs('blackberry') ? 'blackberry' : 60 uaIs('symbian') ? 'symbian' : 61 (uaIs('x11') || uaIs('linux')) ? 'linux' : 62 uaIs('mac') ? 'mac' : 63 uaIs('win') ? 'win' : ''; 64 65 var classNames = [ie, os, 'js']; 66 67 var agents = ua.split(' '); 68 69 if (isMobile()) { 70 classNames.push('mobile'); 71 } 72 73 $.each(agents, function () { 74 if (!this.match(/(\w+)\/([^\s]+)/)) { 75 return; 76 } 77 78 var agent = RegExp.$1; 79 var fullVersion = RegExp.$2.replace(/[\/\.]/g, '_'); 80 var majorVersion = fullVersion.replace(/_.*/, ''); 81 82 classNames = classNames.concat( 83 [agent, agent + '_' + fullVersion, agent + '_' + majorVersion] 84 ); 85 }); 86 87 // Safari and Opera split the user agent into Safari/<build> and 88 // Version/<version> so rename Version -> (Safari|Opera) 89 $.each(['safari', 'opera'], function (index, browser) { 90 if ( 91 $.inArray(browser, classNames) !== -1 && 92 $.inArray('version', classNames) !== -1 93 ) { 94 classNames = $.map(classNames, function (c) { 95 return c.replace('version', browser); 96 }); 97 } 98 }); 99 100 // Chrome incorrectly identifies itself as Safari 101 if ($.inArray('chrome', classNames) !== -1) { 102 classNames = $.map(classNames, function (c) { 103 return c.match(/^safari/) ? null : c; 104 }); 105 } 106 107 var engineVersion = ( 108 // From jQuery 1.2.6 109 (ua.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [0,'0'])[1] 110 ).replace(/\./g, '_'); 111 112 $.each(['opera', 'applewebkit', 'gecko'], function () { 113 if ($.inArray(String(this), classNames) !== -1) { 114 classNames.push(this + '_' + engineVersion); 115 return false; 116 } 117 }); 118 119 // Deduplicate. $.unique is only for DOM elements :-( 120 var seen = {}; 121 classNames = $.map(classNames, function (c) { 122 // Mozilla so abused it's pretty much useless 123 if (seen[c] || c.match(/^mozilla/)) { 124 return null; 125 } else { 126 seen[c] = true; 127 return c; 128 } 129 }).join(' '); 130 131 // Add to the html element now to avoid any FOUC 132 var html = $('html'); 133 html.addClass(classNames); 134 135 $(function () { 136 // Remove our platform specific classes 137 html.removeClass(classNames); 138 139 // Check whether html element had preexisting classes 140 if (!html.attr('class')) 141 html.removeAttr('class'); 142 143 // Add our platform specific classes to body 144 $('body').addClass(classNames); 145 }); 146 } 147 }); 148 149 $.platformSelector(); 150})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.