The Piece of Code

[javascript] 모바일 기기 구분하기

fonch 2013. 2. 28. 00:48

 

> 기본 코드


Javascript 에서 현재 페이지를 호출한 기기(브라우저)가 어떤 기기(브라우저)인지 판별하는 소스입니다.

객체지향이죠? ㅎㅎㅎ

외국 블로그에서 퍼왔습니다..

사용법을 예로 들면 isMobile.iOS() 처럼 사용하시면 됩니다. iOS 일경우는 true 아니면 false 로 반환됩니다.


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

$ 출처: http://www.abeautifulsite.net/blog/2011/11/detecting-mobile-devices-with-javascript/

 




> jQuery에 객체 확장하여 사용하기.


다음처럼 jQuery extend 시켜서 사용할수도 있습니다. ^^;;

(any 는 추가가 안되네요… if 걸어서 하드코딩해야 하나봐요.. ㅠㅠ;;)


(function ($) {
        $.browser = $.extend({
                 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)
                 }
        });
})(jQuery);


'The Piece of Code' 카테고리의 다른 글

[C#] Image Resize - Winfrom  (0) 2016.02.10
[CSS] margin, pdding 순서  (0) 2015.02.03
[C#] StripHtml  (1) 2013.01.20
[Windows 8, C#] MessageDialog  (0) 2013.01.20
[C#] Base64 Encoding, Decoding  (0) 2012.12.07