2011-06-28 8 views
7

¿Alguien sabe de una manera buena y confiable de averiguar la versión del tipo & de un navegador instalado en el cliente usando JavaScript/jQuery?Buscar el tipo y la versión del navegador?

Parece que jQuery tiene algunas funciones incorporadas, pero tiene problemas para detectar Chrome. ¿Alguna otra forma confiable de hacerlo?

+1

La detección por qué razón? – Gareth

Respuesta

8

Si desea información sobre el navegador que utiliza su visitante y lo utiliza para estadísticas o para mostrar información al usuario, puede usar el jQuery Browser Plugin.

Se le da un objeto en Javascript que contiene toda la información sobre el navegador que se utilice.

Asegúrese de do feature detection en lugar de la detección del navegador cuando se quiere determinar si una determinada característica está disponible en un navegador, aplicar correcciones de errores, etc.

No hay necesidad de reinventar la rueda.

+0

parece que jQuery Browser Plugin es el más adecuado para mi situación. Gracias por la información sobre detección de características. – Arjun

7

Enfoque 1:
Nota:Desde jQuery 1.3, jQuery.browser es obsoleto

Prueba esto:

<!DOCTYPE html> 
<html> 
<head> 
<style> 
p { color:green; font-weight:bolder; margin:3px 0 0 10px; } 
div { color:blue; margin-left:20px; font-size:14px; } 
span { color:red; } 
</style> 
<script src="http://code.jquery.com/jquery-latest.js"></script> 
</head> 
<body> 

<p>Browser info: (key : value)</p> 

<script> 
    jQuery.each(jQuery.browser, function(i, val) { 
    $("<div>" + i + " : <span>" + val + "</span>") 
       .appendTo(document.body); 
    });</script> 

</body> 
</html> 

Enfoque 2:

 

// A quick solution without using regexp (to speed up a little). 
var userAgent = navigator.userAgent.toString().toLowerCase(); 
if ((userAgent.indexOf('safari') != -1) && !(userAgent.indexOf('chrome') != -1)) { 
alert('We should be on Safari only!'); 
} 
 
+2

Y como un conveniente JsFiddle: http://jsfiddle.net/dZs7D/ – configurator

+0

Cuando ejecuto este programa en Chrome, los siguientes son los resultados: Información del navegador: (clave: valor) webkit: true versión: 534.7 safari: true – Arjun

+0

¿Hay alguna forma de differentiage b/w chrome y safari? – Arjun

1

Desde $.browser se retira en jQuery 1.9, utilice esto:

// ---------------------------------------------------------- 
// A short snippet for detecting versions of IE in JavaScript 
// without resorting to user-agent sniffing 
// ---------------------------------------------------------- 
// If you're not in IE (or IE version is less than 5) then: 
//  ie === undefined 
// If you're in IE (>=5) then you can determine which version: 
//  ie === 7; // IE7 
// Thus, to detect IE: 
//  if (ie) {} 
// And to detect the version: 
//  ie === 6 // IE6 
//  ie > 7 // IE8, IE9 ... 
//  ie < 9 // Anything less than IE9 
// ---------------------------------------------------------- 

// UPDATE: Now using Live NodeList idea from @jdalton 

var ie = (function(){ 

    var undef, 
     v = 3, 
     div = document.createElement('div'), 
     all = div.getElementsByTagName('i'); 

    while (
     div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->', 
     all[0] 
    ); 

    return v > 4 ? v : undef; 

}()); 
+0

Un poco específico para IE. – Stephan

+0

@Alex Aún más confiable que el rastreo de agente de usuario. – Johan

+0

¿Puede completar su respuesta con la detección del navegador para otros navegadores importantes, por favor? – Stephan

Cuestiones relacionadas