Browser detection using Jquery

It is very easy to detect the browser using jquery. This code snipt will give you the browser.

var browser = GetBrowser();
alert(browser) ;

function GetBrowser()
$ = /chrome/.test(navigator.userAgent.toLowerCase());
if($ return “chrome”;
if($.browser.mozilla) return “mozilla”;
if($.browser.opera) return “opera”;
if($.browser.safari) return “safari”;
if($.browser.msie) return “ie”;


Demo can be found here

Please note that browser detection has been deprecated as of jquery 1.3 , but the deprecated properties probably won’t be removed from jQuery anytime soon. And when they will be removed, if you still absolutely need to do browser detection, you can add the same functionality easily with this code

(function($) {
var userAgent = navigator.userAgent.toLowerCase();

$.browser = {
version: (userAgent.match( /.+(?:rv|it|ra|ie)[/: ]([d.]+)/ ) || [0,’0′])[1],
safari: /webkit/.test( userAgent ),
opera: /opera/.test( userAgent ),
msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )



Leave a Reply

Your email address will not be published. Required fields are marked *