Last Updated: April 23, 2019
·
1.312K
· nampnq

Javascript detect mobile platform

Javascript detect mobile platform

utils.isMobile = {
         Android: function() {
             return navigator.userAgent.match(/Android/i);
         },
         BlackBerry: function() {
             return navigator.userAgent.match(/BlackBerry/i);
         },
         iPad: function(){
             return navigator.userAgent.match(/iPad/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) || navigator.userAgent.match(/WPDesktop/i);
         },
         any: function() {
             return (utils.isMobile.Android() || utils.isMobile.BlackBerry() || utils.isMobile.iOS() || utils.isMobile.Opera() || utils.isMobile.Windows());
         }
     }

1 Response
Add your response

Are you suggesting this as a good thing?

Do you prefer it over feature detection?

I do use something similar to fix edge cases but providing code like this should come with a caveat that there are better ways to solve the actual problem.

over 1 year ago ·