Advertisement
Guest User

BrowserDetect

a guest
Mar 18th, 2011
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // Checks the browser and adds classes to the body to reflect it.
  3.  
  4. $(document).ready(function(){
  5.    
  6.     var userAgent = navigator.userAgent.toLowerCase();
  7.     $.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
  8.    
  9.     // Is this a version of IE?
  10.     if($.browser.msie){
  11.         $('body').addClass('msie');
  12.        
  13.         // Add the version number
  14.         $('body').addClass('msie' + $.browser.version.substring(0,1));
  15.     }
  16.    
  17.    
  18.     // Is this a version of Chrome?
  19.     if($.browser.chrome){
  20.    
  21.         $('body').addClass('chrome');
  22.        
  23.         //Add the version number
  24.         userAgent = userAgent.substring(userAgent.indexOf('chrome/') +7);
  25.         userAgent = userAgent.substring(0,1);
  26.         $('body').addClass('chrome' + userAgent);
  27.        
  28.         // If it is chrome then jQuery thinks it's safari so we have to tell it it isn't
  29.         $.browser.safari = false;
  30.     }
  31.    
  32.     // Is this a version of Safari?
  33.     if($.browser.safari){
  34.         $('body').addClass('safari');
  35.        
  36.         // Add the version number
  37.         userAgent = userAgent.substring(userAgent.indexOf('version/') +8);
  38.         userAgent = userAgent.substring(0,1);
  39.         $('body').addClass('safari' + userAgent);
  40.     }
  41.    
  42.     // Is this a version of Mozilla?
  43.     if($.browser.mozilla){
  44.        
  45.         //Is it Firefox?
  46.         if(navigator.userAgent.toLowerCase().indexOf('firefox') != -1){
  47.             $('body').addClass('firefox');
  48.            
  49.             // Add the version number
  50.             userAgent = userAgent.substring(userAgent.indexOf('firefox/') +8);
  51.             userAgent = userAgent.substring(0,1);
  52.             $('body').addClass('firefox' + userAgent);
  53.         }
  54.         // If not then it must be another Mozilla
  55.         else{
  56.             $('body').addClass('mozilla');
  57.         }
  58.     }
  59.    
  60.     // Is this a version of Opera?
  61.     if($.browser.opera){
  62.         $('body').addClass('opera');
  63.     }
  64.    
  65.    
  66. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement