Now a days, mobile is a common device in everyone’s hand. Internet is also used very much using these devices. So for web world, It is a challenge to be compatible their websites with these devices. For that, every application should be detect mobile device. Every language has its own method to detect the mobile device but we are explaining here the way of detecting mobile devices using jQuery or JAVASCRIPT.
Detecting Mobile Devices using JAVASCRIPT: We will use here navigator’s useragent property which tell us about the device.
var isMobile = { iOS: function() { return navigator.userAgent.match(/iPhone|iPad|iPod/i); }, Android: function() { return navigator.userAgent.match(/Android/i); }, BlackBerry: function() { return navigator.userAgent.match(/BlackBerry/i); }, Opera: function() { return navigator.userAgent.match(/Opera Mini/i); }, Windows: function() { return navigator.userAgent.match(/IEMobile/i); }, any: function() { return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows()); } };
Now we can check about Mobile Device by using this function:
if(isMobile.any()) { alert("This is a Mobile Device"); }
If we want to check about iPhone/iPad/iPod, use this:
if(isMobile.iOS()) { alert("This is a iPhone/iPad/iPod Device"); }
2. Another JavaScript way to Detect Mobile Devices
We can try under given function in simple and short way also :
function detectmob() { return !navigator.userAgent.match(/iPad|iPhone|Android|BlackBerry|Windows Phone|webOS/i); }
function detectmob() { return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent); }