X

LATEST #JQUERY NEWS

How to Detect Browser Using jQuery


One of the more frustrating parts of creating a new web project is making sure that it looks the same (or as close to the same as humanly possible) across all different browser types. The problem with achieving cross-browser compatibility is that each browser has its own set of inherent styling rules, and additionally, some browsers (especially older versions of most browsers) don’t offer support for certain CSS properties and rules. Because we can’t control what browsers our users choose to use to visit our sites, as developers, we have to do our best to make sure that our projects look great on all browsers.

To do this, we pretty much have to be able to single out a certain type of browser and apply code based on whether or not our sites are being used in that particular browser. There are basically two ways of doing that. You can include CSS rules called “browser hacks” that will allow you to apply CSS code to certain elements based on what browser is being used, or, if you want your code to be a little more dynamic, you can use jQuery code to detect which browser is being used, and then add conditional code (maybe changes to content or CSS) from there.

Below is a snippet that can be used to detect around 20 different types of popular (and some less popular) web browsers. Use this snippet to determine which browser is being used to view your site, then go ahead and add your desired conditional code that will make sure your site looks great on all browsers.

function _setBrowser()
{
 var userAgent = navigator.userAgent.toLowerCase();
 // Figure out what browser is being used
 jQuery.browser = {
 version: (userAgent.match( /.+(?:rv|it|ra|ie|me|ve)[\/: ]([\d.]+)/ ) || [])[1],
 chrome: /chrome/.test( userAgent ),
 safari: /webkit/.test( userAgent ) && !/chrome/.test( userAgent ),
 opera: /opera/.test( userAgent ),
 firefox: /firefox/.test( userAgent ),
 msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
 mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent ),
 webkit: $.browser.webkit,
 gecko: /[^like]{4} gecko/.test( userAgent ),
 presto: /presto/.test( userAgent ),
 xoom: /xoom/.test( userAgent ),
 android: /android/.test( userAgent ),
 androidVersion: (userAgent.match( /.+(?:android)[\/: ]([\d.]+)/ ) || [0,0])[1],
 iphone: /iphone|ipod/.test( userAgent ),
 iphoneVersion: (userAgent.match( /.+(?:iphone\ os)[\/: ]([\d_]+)/ ) || [0,0])[1].toString().split('_').join('.'),
 ipad: /ipad/.test( userAgent ),
 ipadVersion: (userAgent.match( /.+(?:cpu\ os)[\/: ]([\d_]+)/ ) || [0,0])[1].toString().split('_').join('.'),
 blackberry: /blackberry/.test( userAgent ),
 winMobile: /Windows\ Phone/.test( userAgent ),
 winMobileVersion: (userAgent.match( /.+(?:windows\ phone\ os)[\/: ]([\d_]+)/ ) || [0,0])[1]
 };
 jQuery.browser.mobile = ($.browser.iphone || $.browser.ipad || $.browser.android || $.browser.blackberry );
};

Recently Published

article image
»

How to Dynamically Trim Text Using jQuery

This easy beginners jQuery tutorial demonstrates how you can use ...

article image
»

jQuery Snippets: Getting the Current Year

Here’s a fun, simple, and incredibly useful jQuery that you can ...

article image
»

jQuery Snippets: How to Conditionally Enable Submit

One easy way to prevent users from submitting an empty form in a ...

article image
»

How to Use Google Analytics with jQuery Mobile

jQuery Mobile is an HTML5 based web-page framework that’s ...

article image
»

jQuery Snippets: Using jQuery to Check All Boxes

When you’ve given your users a ton of┬ácheckbox input fields ...

article image
»

Useful jQuery Code Snippets for Working with Images

Images are an essential part of any website and sometimes working ...

article image
»

How to Use jQuery to Check if Cookies are Enabled

A cookie (not the kind with chocolate chips) is a small piece of data ...

article image
»

A Beginner’s Guide to jQuery’s Selectors

We talk a lot about event methods on this site, but not so much about ...

article image
»

Add Keyboard Shortcut to Anchor Tag Using jQuery

Making a user friendly website can be quite a challenging task. There ...