Javascript – detect what type of mobile

  |   By  |  0 Comments

For one of my apps, I have a landing page website – ourchurchapp.online, that detects if a mobile phone or tablet has accessed it and redirects to the correct app store. I use a javascript function to detect iOS, Android, Windows and Amazon – although Windows doesn’t go anywhere – so few people use it on mobile devices, I haven’t bothered developing for it.

function getMobileOperatingSystem() {
  var userAgent = navigator.userAgent || navigator.vendor || window.opera;
  var os="Unknown";
   
      // Windows Phone must come first because its UA also contains "Android"
    if (/windows phone/.test(navigator.userAgent)) {
        os= "Windows Phone";
    }
  	/* Kindle before Android as the UA contains Android too */
    if(/Kindle|Silk|KFAPW|KFARWI|KFASWI|KFFOWI|KFJW|KFMEWI|KFOT|KFSAW|KFSOWI|KFTBW|KFTHW|KFTT|WFFOWI|KFFOWI/.test(navigator.userAgent))
    {
        os= "Amazon";
    }
    if (/android/.test(navigator.userAgent)) {
        os= "Android";
    }
    // iOS detection from: http://stackoverflow.com/a/9039885/177710
    if (/iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream) {
       os = "iOS";
    }
   
    return os;
}
name

ABOUT THE AUTHOR - ANDY MOYLE

Andy Moyle is a church leader and web developer. His biggest project is the Church Admin WordPress plugin and app. He also runs, mainly so he can eat pizza.