Ver Mensaje Individual
  #20 (permalink)  
Antiguo 07/05/2012, 03:26
gebremswar
 
Fecha de Ingreso: enero-2012
Ubicación: Santiago de Surco, Lima - Perú
Mensajes: 266
Antigüedad: 12 años, 10 meses
Puntos: 57
Información Respuesta: Duda sobre como detectar navegador y versión

Esto es lo que el tema pedía. Pensé que alguien lo pondría en mi ausencia; pero bueno aquí está
Código Javascript:
Ver original
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>@GBreagan</title>
  6. <script type='text/javascript'>
  7. /**
  8.  * Browser properties:
  9.  * -fullName (Opera, Google Chrome, Mozilla Firefox...)
  10.  * -name (opera, chrome, firefox...)
  11.  * -code (op, ch, ff...)
  12.  * -fullVersion (5.0.3.12...)
  13.  * -version (5)
  14.  * -mobile (true, false)
  15.  * -platform (windows 7, Linux, Machintosh...)
  16.  * @author Rbrt
  17.  * @version 2.1.5
  18.  * @returns Browser object
  19.  */
  20. function Browser() {   
  21.     // ---- public properties -----
  22.     this.fullName = 'unknown'; // getName(false);
  23.     this.name = 'unknown'; // getName(true);
  24.     this.code = 'unknown'; // getCodeName(this.name);
  25.     this.fullVersion = 'unknown'; // getVersion(this.name);
  26.     this.version = 'unknown'; // getBasicVersion(this.fullVersion);
  27.     this.mobile = false; // isMobile(navigator.userAgent);
  28.     this.width = screen.width;
  29.     this.height = screen.height;
  30.     this.platform =  'unknown'; //getPlatform(navigator.userAgent);
  31.    
  32.     // ------- init -------    
  33.     this.init = function() { //operative system, is an auxiliary var, for special-cases
  34.         //the first var is the string that will be found in userAgent. the Second var is the common name
  35.         // IMPORTANT NOTE: define new navigators BEFORE firefox, chrome and safari
  36.         var navs = [
  37.             { name:'Opera Mobi', fullName:'Opera Mobile', pre:'Version/' },
  38.             { name:'Opera Mini', fullName:'Opera Mini', pre:'Version/' },
  39.             { name:'Opera', fullName:'Opera', pre:'Version/' },
  40.             { name:'MSIE', fullName:'Microsoft Internet Explorer', pre:'MSIE ' },  
  41.             { name:'BlackBerry', fullName:'BlackBerry Navigator', pre:'/' },
  42.             { name:'BrowserNG', fullName:'Nokia Navigator', pre:'BrowserNG/' },
  43.             { name:'Midori', fullName:'Midori', pre:'Midori/' },
  44.             { name:'Kazehakase', fullName:'Kazehakase', pre:'Kazehakase/' },
  45.             { name:'Chromium', fullName:'Chromium', pre:'Chromium/' },
  46.             { name:'Flock', fullName:'Flock', pre:'Flock/' },
  47.             { name:'Galeon', fullName:'Galeon', pre:'Galeon/' },
  48.             { name:'RockMelt', fullName:'RockMelt', pre:'RockMelt/' },
  49.             { name:'Fennec', fullName:'Fennec', pre:'Fennec/' },
  50.             { name:'Konqueror', fullName:'Konqueror', pre:'Konqueror/' },
  51.             { name:'Arora', fullName:'Arora', pre:'Arora/' },
  52.             { name:'Swiftfox', fullName:'Swiftfox', pre:'Firefox/' },
  53.             { name:'Maxthon', fullName:'Maxthon', pre:'Maxthon/' },
  54.             // { name:'', fullName:'', pre:'' } //add new broswers
  55.             // { name:'', fullName:'', pre:'' }
  56.             { name:'Firefox',fullName:'Mozilla Firefox', pre:'Firefox/' },
  57.             { name:'Chrome', fullName:'Google Chrome', pre:'Chrome/' },
  58.             { name:'Safari', fullName:'Apple Safari', pre:'Version/' }
  59.         ];
  60.    
  61.         var agent = navigator.userAgent, pre;
  62.         //set names
  63.         for (i in navs) {
  64.             if (agent.indexOf(navs[i].name)>-1) {
  65.                 pre = navs[i].pre;
  66.                 this.name = navs[i].name.toLowerCase(); //the code name is always lowercase
  67.                 this.fullName = navs[i].fullName;
  68.                 if (this.name=='msie') this.name = 'iexplorer';
  69.                 if (this.name=='opera mobi') this.name = 'opera';
  70.                 if (this.name=='opera mini') this.name = 'opera';
  71.                 break; //when found it, stops reading
  72.             }
  73.         }//for
  74.        
  75.       //set version
  76.         if ((idx=agent.indexOf(pre))>-1) {
  77.             this.fullVersion = '';
  78.             this.version = '';
  79.             var nDots = 0;
  80.             var len = agent.length;
  81.             var indexVersion = idx + pre.length;
  82.             for (j=indexVersion; j<len; j++) {
  83.                 var n = agent.charCodeAt(j);
  84.                 if ((n>=48 && n<=57) || n==46) { //looking for numbers and dots
  85.                     if (n==46) nDots++;
  86.                     if (nDots<2) this.version += agent.charAt(j);
  87.                     this.fullVersion += agent.charAt(j);
  88.                 }else j=len; //finish sub-cycle
  89.             }//for
  90.             this.version = parseInt(this.version);
  91.         }
  92.        
  93.         // set Mobile
  94.         var mobiles = ['mobi', 'mobile', 'mini', 'iphone', 'ipod', 'ipad', 'android', 'blackberry'];
  95.         for (var i in mobiles) {
  96.             if (agent.toLowerCase().indexOf(mobiles[i])>-1) this.mobile = true;
  97.         }
  98.         if (this.width<700 || this.height<600) this.mobile = true;
  99.        
  100.         // set Platform        
  101.         var plat = navigator.platform;
  102.         if (plat=='Win32' || plat=='Win64') this.platform = 'Windows';
  103.         if (agent.indexOf('NT 5.1') !=-1) this.platform = 'Windows XP';        
  104.         if (agent.indexOf('NT 6') !=-1)  this.platform = 'Windows Vista';
  105.         if (agent.indexOf('NT 6.1') !=-1) this.platform = 'Windows 7';
  106.         if (agent.indexOf('Mac') !=-1) this.platform = 'Macintosh';
  107.         if (agent.indexOf('Linux') !=-1) this.platform = 'Linux';
  108.         if (agent.indexOf('iPhone') !=-1) this.platform = 'iOS iPhone';
  109.         if (agent.indexOf('iPod') !=-1) this.platform = 'iOS iPod';
  110.         if (agent.indexOf('iPad') !=-1) this.platform = 'iOS iPad';
  111.         if (agent.indexOf('Android') !=-1) this.platform = 'Android';
  112.        
  113.         if (this.name!='unknown') {
  114.             this.code = this.name+'';
  115.             if (this.name=='opera') this.code = 'op';
  116.             if (this.name=='firefox') this.code = 'ff';
  117.             if (this.name=='chrome') this.code = 'ch';
  118.             if (this.name=='safari') this.code = 'sf';
  119.             if (this.name=='iexplorer') this.code = 'ie';
  120.             if (this.name=='maxthon') this.code = 'mx';
  121.         }
  122.        
  123.         //manual filter, when is so hard to define the navigator type
  124.         if (this.name=='safari' && (this.platform=='Linux' || this.platform=='Android')) {
  125.             this.name = 'unknown';
  126.             this.fullName = 'unknown';
  127.             this.code = 'unknown';
  128.         }
  129.        
  130.         if (this.name=='unknown') {
  131.             if (agent.toLowerCase().indexOf('webkit')) this.fullName = 'unknown webkit navigator';
  132.         }
  133.        
  134.     };//function
  135.    
  136.     this.init();
  137.  
  138. }//Browser class
  139.  
  140.  
  141.       var brw = new Browser();
  142.       alert('fullName: ' + brw.fullName + '\n' + 'name: ' + brw.name + '\n' + 'fullVersion: ' + brw.fullVersion + '\n' + 'version: ' + brw.version + '\n' + 'platform: ' + brw.platform+ '\n' + 'mobile: ' + brw.mobile+ '\n' + 'resolution: ' + brw.width + 'x' + brw.height);
  143.     </script>
  144.   </head>
  145. </html>