Browser detection using Jquery

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

[javascript]
var browser = GetBrowser();
alert(browser) ;
})

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

}
[/javascript]

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

[javascript]
(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 )
};

})(jQuery);
[/javascript]

Vivek

Leave a Reply

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