Skip to content

Instantly share code, notes, and snippets.

@stphn
Forked from 4ndrej/detect.js
Created October 12, 2019 12:46
Show Gist options
  • Save stphn/36fe196fa63e0bb9c670453bac4a0846 to your computer and use it in GitHub Desktop.
Save stphn/36fe196fa63e0bb9c670453bac4a0846 to your computer and use it in GitHub Desktop.
detect mobile device using javascript
var isMobile = {
Android: function() {
return navigator.userAgent.match(/Android/i);
},
BlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
return navigator.userAgent.match(/iPhone|iPad|iPod/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());
},
none: function() {
return (!isMobile.any());
}
};
@stphn
Copy link
Author

stphn commented Oct 12, 2019

if (isMobile.any()){
        document.write('Mobile');
        if (isMobile.iOS()){
            document.write('/iOS');
        } else if (isMobile.Android()){
            document.write('/Android');
        } else if (isMobile.BlackBerry()){
            document.write('/BlackBerry');
        } else if (isMobile.Windows()){
            document.write('/Windows');
        } else {
            document.write('/other');
        }
    } else if (isMobile.none()) {
        document.write('non-mobile');
    } else {
        document.write('unknown');
    }

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