Skip to content

Instantly share code, notes, and snippets.

@arakno
Created February 6, 2015 12:08
Show Gist options
  • Save arakno/8f1841744ee9240cd9ba to your computer and use it in GitHub Desktop.
Save arakno/8f1841744ee9240cd9ba to your computer and use it in GitHub Desktop.
mobile browser UA/feature detection sniffing
var ua = navigator.userAgent.toLowerCase(),
isOpera = !!window.opera || ua.indexOf(' opr/') >= 0,
isChrome = !!window.chrome && !isOpera,
isAndroid = ua.indexOf("android") > -1,
iOS = /(ipad|iphone|ipod)/g.test(ua),
isHandheld = window.matchMedia && window.matchMedia('(max-device-width: 960px)').matches || screen.width <= 960,
isTouchDevice = 'ontouchstart' in window || 'onmsgesturechange' in window,
isMobile = (isHandheld && isTouchDevice) && (isAndroid || iOS);
@arakno
Copy link
Author

arakno commented Feb 6, 2015

        var ua = navigator.userAgent.toLowerCase(),
            isOpera = !!window.opera || ua.indexOf(' opr/') >= 0,
            isChrome = !!window.chrome && !isOpera,
            isAndroid = ua.indexOf("android") > -1,
            iOS = /(ipad|iphone|ipod)/g.test(ua),
            isWM = ua.match(/IEMobile/i) || ua.match(/WPDesktop/i),
            isMobileBrowser = ua.match(/Mobi/i),
            //feature detect
            isOrientation = typeof window.orientation !== 'undefined',
            isHandheld = window.matchMedia && window.matchMedia('(max-device-width: 960px)').matches || screen.width <= 960,
            isTouchDevice = 'ontouchstart' in window || 'onmsgesturechange' in window,//Modernizr.touch
            isMobile = (isOrientation && isHandheld && isTouchDevice) && (isAndroid || iOS || isWM) && isMobileBrowser;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment