HTML5

Dectecting Mobile Devices with Javascript

미랭군 2012. 12. 4. 11:00

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());
    }
};

example

if(isMobile.any()) {
  
//some code...
}

if( isMobile.iOS() ) alert('iOS');

 

만약 모바일이라는 의미가 작은 스크린을 의미한다면..

var windowWidth = window.screen.width < window.outerWidth ? window.screen.width : window.outerWidth;
var mobile = windowWidth < 500;

 

이렇게 간단한 로직을 추가해도 좋을 듯 하다.