Detecting a mobile browser

In the growing mobile internet world, it is necessary to make your application mobile browser friendly.

I am working on client project where client has some specific requirement when application is open in mobile device browser. I have to show/hide CSS class based on browser detection. I was googling for solution to detect mobile device/browser using JavaScript or jQuery. I found the solution which works great for me.



Minified version of script included in the HTML since it’s <1kb.

// Minified version of isMobile included in the HTML since it’s <1kb
(function(i){var e=/iPhone/i,n=/iPod/i,o=/iPad/i,t=/(?=.*\bAndroid\b)(?=.*\bMobile\b)/i,r=/Android/i,d=/BlackBerry/i,s=/Opera Mini/i,a=/IEMobile/i,b=/(?=.*\bFirefox\b)(?=.*\bMobile\b)/i,h=RegExp(“(?:Nexus 7|BNTV250|Kindle Fire|Silk|GT-P1000)”,”i”),c=function(i,e){return i.test(e)},l=function(i){var l=i||navigator.userAgent;{phone:c(e,l),ipod:c(n,l),tablet:c(o,l),device:c(e,l)||c(n,l)||c(o,l)},{phone:c(t,l),tablet:!c(t,l)&&c(r,l),device:c(t,l)||c(r,l)},this.other={blackberry:c(d,l),opera:c(s,l),windows:c(a,l),firefox:c(b,l),device:c(d,l)||c(s,l)||c(a,l)||c(b,l)},this.seven_inch=c(h,l),||||this.other.device||this.seven_inch},v=i.isMobile=new l;v.Class=l})(window);

// My own arbitrary use of isMobile, as an example
(function () {
var MOBILE_SITE = ‘/mobile/index.html’, // site to redirect to
NO_REDIRECT = ‘noredirect’; // cookie to prevent redirect

// I only want to redirect iPhones, Android phones and a handful of 7″ devices
if ( || || isMobile.seven_inch) {

// Only redirect if the user didn’t previously choose
// to explicitly view the full site. This is validated
// by checking if a “noredirect” cookie exists
if ( document.cookie.indexOf(NO_REDIRECT) === -1 ) {
document.location = MOBILE_SITE;

Happy Coding πŸ™‚


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s