Javascript Mobile Check
Check if the browser being used is mobile device.
This is a javascript snippet that checks if the browser being used is on a mobile phone. This does not include tablets.
var phone = { getUserAgent: function() { return navigator.userAgent; }, isAndroid: function() { return this.getUserAgent().match(/Android/i); }, isBlackBerry: function() { return this.getUserAgent().match(/BlackBerry/i); }, isIOS: function() { return this.getUserAgent().match(/iPhone|iPad|iPod/i); }, isOpera: function() { return this.getUserAgent().match(/Opera Mini/i); }, isWindows: function() { return this.isWindowsDesktop() || this.isWindowsMobile(); }, isWindowsMobile: function() { return this.getUserAgent().match(/IEMobile/i); }, isWindowsDesktop: function() { return this.getUserAgent().match(/WPDesktop/i); ; }, isAny: function() { var foundAny = false; var getAllMethods = Object.getOwnPropertyNames(phone).filter(function(property) { return typeof phone[property] == 'function'; }); for (var index in getAllMethods) { if (getAllMethods[index] === 'getUserAgent' || getAllMethods[index] === 'isAny' || getAllMethods[index] === 'isWindows') { continue; } if (phone[getAllMethods[index]]()) { foundAny = true; break; } } return foundAny; } };
How to implement?
if (phone.isAndroid()) { // write what you want to happen when phone is android }; if (phone.isIOS()) { // write what you want to happen when phone is iPhone }; if (phone.isAny()) { // write what you want to happen when its any phone };